FUNCTIONS
FUNCTION OVERVIEW :
- A FUNCTION is a self-contained program segment that carries out some specific, well-defined task.
- Every C program consists of one or more functions.
- One of these functions must be called main.
- Execution of the program will always begin by carrying out the instructions in main.
- Additional functions will be subordinate to main, and perhaps to one another.
- A FUNCTION will process information that is passed to it from the calling portion of the program, and return a single value.
DEFINING AND ACCESSING A FUNCTION :
- DEFINIG A FUNCTION :
-
SYNTAX :
return_data_type function_name(arguments) { //STATEMENTS LIST; return value; }
-
C PROGRAMS FUNCTION consists of these parts :
-
return_data_type :
A FUNCTION can return a value,the return_data_type is the data_type of tge value which is returned.
-
function_name :
The actual name of the FUNCTION which we have defined.
-
arguments :
Arguments are nothing but parameters , the value which we pass with the functions in the braces after function name.
They are OPTIONAL to define. -
statements :
Thay are the tasks to be performed inside the function body.
-
return :
It returns the value according to the data types.
DATA TYPE void returns nothing.
-
return_data_type :
- All the other functions in C needs to be defined after main() function.
-
SYNTAX :
- ACCESSING A FUNCTION :
- To use a function that we have declared , we will have to call that function to perform the defined task.
- To call a FUNCTION, we have to pass the required parameters along with the function name, and if the function returns a value, then we can store the returned value.
- We can call the FUNCTION using 2 ways : CALL BY VALUE and CALL BY REFERNCE.
//use of a function method 1
#include <stdio.h>
void display()
{
printf("HELLO");
}
void main()
{
printf("CALLING DISPLAY FUNCTION");
display();
printf("DONE");
}
OR
//use of a function method 2
#include <stdio.h>
void display();
void main()
{
printf("CALLING DISPLAY FUNCTION");
display();
printf("DONE");
}
void display()
{
printf("HELLO");
}
RECURSIVE FUNCTION :
-
SYNTAX :
void recursive() { recursive(); } void main() { recursive(); }
- The function which call itself,it is called as RECURSIVE FUNCTIONS.
- While defining RECURSIVE FUNCTION we also have to define the exit condition.
- RECURSIVE FUNCTION will go to INFINE LOOPING unit an exit condition is defined.
- This types of FUNCTIONS are mainly used for solving mathematical problems.
//use of recursive function
#include <stdio.h>
int sum(int num)
{
if(num!=0)
return num+sum(num-1);
else
return num;
}
void main()
{
int n,result;
printf("ENTER NUMBER : ");
scanf("%d",&n);
result=sum(n);
printf("RESULT IS : ",result);
}
FUNCTION CALLS :
- CALL BY VALUE :
- The CALL BY VALUE method of passing ARGUMENTS to a function copies the actual value of an argument into the parameter of the function.
- Here changes made to the parameter inside the function have no effect on the argument.
- By default, C programming uses CALL BY VALUE to pass arguments.
- CALL BY REFERENCE :
- The CALL BY REFERENCE method of passing arguments to a function copies the address of an argument into the parameter.
- Inside the function, the address is used to access the actual argument used in the call.
- It means the changes made to the parameter affect the passed argument.
//call by value
#include <stdio.h>
void change(int a)
{
printf("VALUE IS : %d ",a);
a=a+100;
printf("VALUE IS : %d ",a);
}
void main()
{
int x=100;
printf("ORIGINAL VALUE : %d",x);
change(x);
printf("AFTER CALLING A FUNCTION : %d ",x);
}
//call by reference
#include <stdio.h>
void change(int *a)
{
printf("VALUE IS : %d ",*a);
*a=a+100;
printf("VALUE IS : %d ",*a);
}
void main()
{
int x=100;
printf("ORIGINAL VALUE : %d",x);
change(&x);
printf("AFTER CALLING A FUNCTION : %d ",x);
}
Comments
Post a Comment