Function
Function is a group of statements which perform a specific task. We divide our program of c in multiple functionality blocks which is called function. whenever we require to perform that functionality we call our function which is written to perform that particular functionality.
below picture gives very good introduction to function role.
Function definition
general form of definition of function is as follow
return_type function_name(parameters)
{
body_of_function;
}
function definition has has following components
1) Return type:-
A function after performing their specified functionality might return some value to caller of that function. so return type tell the data type of returning value.
2) Function name:- function name is the name given to function. Function is called by using of function name by caller.
3) Parameters:- Parameters are the variable which is filled by the caller of that function. Parameters tell the type, order and number of arguments to a function.
4) Function body:- Body of the group of statements which actually perform the specific task.
Function declaration or function prototype:-
function declaration or function prototype tell the compiler about the function name , return type and parameter list.
if we call the function before declaring it we will come across compile time error because compiler will not be aware about the existence of such function.
function declaration consist of following parts
1) return type
2)function name
3)parameter list
example:- void fun(void);
above statement declare a function fun which return nothing and even does not take any arguments.
Calling a function:
To perform a specific work by a defined function we need to call that function in our program.
when we call a function our program control jumps to definition of called function and after executing the function control return back to the called function.
types of function :-
funtion is of two types:
1)Library function
2)user defined function
Library function:- These functions are already defined in library by compiler writer. We just need to include the header file(.h) in pur program to use that function. example printf,scanf etc.
user defined function:- these functions are written by user according to their requirement. Suppose a user wants to write a function for adding of two number.
example:-
int add(int a, int b)
{
return a+b;
}
Types of user defined functions
user defined function can be categorized in below category
1) function with no arguments and no return value
void fun(void);
2) function with no argument but return some value
int fun(int)
3)function with argument but no return value
void fun (int a);
4)function with argument and return value
int fun(int a);
Types of calling a function
Depending upon the way of passing of arguments , calling can be of two types.
1) Call by values:- In call by values only value of argumets is passed to called function. If we make any change in any arguments in called funtion then there will not be any effect in the original value of variable in caller funtion.
void main()
{
int a=10;
fun(a);
printf(“%d”,a); //value of a will be 10 as we passed only value of value a to function fun
}
void fun(int a)
{
a=20;
}
2) Call by reference :- In this type of calling we pass address of variable instead of value as an argument to function. Hence we refer to same variable in called function definition also.
if we do any change in the called function then these changes will also effect the value of original variable in calling function.
void main()
{
int a =10;
fun(a);
printf(“%d”,a); //value of a will be 20 as we passed address of variable
}
void fun(int a)
{
a=20;
}