Function in C
What is Function in C ?
Function is some set of statements which executes when it is called and perform a task.
We can call them again and again in the program when we require.
OR
Function is a named block which can be reused when we require it.So,
we can say that it is reusability of code.
In the above image void main() is a calling function and void sum(),void subtract(), void divide() are called functions.
Calling Function
The function who calls the other function is called calling function.
Called Function
The function which is being called by the calling function is a called function.
Function is some set of statements which executes when it is called and perform a task. We can call them again and again in the program when we require.
OR
Function is a named block which can be reused when we require it.So, we can say that it is reusability of code.In the above image void main() is a calling function and void sum(),void subtract(), void divide() are called functions.
Calling Function
The function who calls the other function is called calling function.
Called Function
The function which is being called by the calling function is a called function.
Types of Function
Built-in functions
Functions which are already defined in the C language functions library.
Library functions
OR
Pre-defined functions
OR
Ready made functions
User defined functions
Function which are defined by the user.
Types of user defined function
For better understanding of arguments and return in functions, user-defined functions can be categorised as:
- Function with no arguments and no return value
- Function with no arguments and return value
- Function with arguments but no return value
- Function with arguments and return value.
Stages Of Function
There are three stages of function:-
- Declaration or Prototype of function
- Body or Definition of function
- Calling of function
1. Syntax for declaration/prototype of function
According to above syntax example is ,
int sum(int a, int b);
is a function prototype which provides following information
to the compiler:
1. name of the function is sum()
2. return type of the function is int.
3. two arguments of type int are passed to function.
Function prototype are not needed if user-definition function is written before main() function.
2. Syntax for definition/body of function
return_type function_name(type(1) argument(1),....,type(n) argument(n)) { statement1; statement2; }