User defined Function:
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. a function will process information that is passed to it from the calling portion of the program, and return a single value. Information is passed to the function via special identifiers called arguments (also called parameters), and returned via the return statement.
Elements of the user defined function:
- Function definition
- Function call
- Function declaration
Function definition:
The function definition is an independent program module. The task of a function is defined in the function definition. A function definition shall include the following elements:
- Function name
- Function type
- list of parameters
- local variable declarations
- Function statements
- A return statement Function call:
Function name, Function type and list of parameters, these three elements are in Function header and local variable declarations, Function statements and a return statement, these three elements are in Function body
In order to use the function every function need to invoke it at a proper place in the program. This is known as Function call. A function can be called by simply using the function name followed by a list of parameters.
Function declaration:
Like variables, all functions in a C program must be declared , before they are invoked. A function declaration (also known as function prototype) consists of four parts:
- Function type(return type)
- Function name
- Parameter List
- Terminating semicolon
Example:
#include< stdio.h >
int add(int a, int b); //Function Prototype
int main()
{
int p,q,r; //local variable
scanf(“%d %d”,&p,&q);
//function call
r=add(p,q); // here p and q are actual parameters
printf(“%d”,r);
return 0;
}
//Function Definition
int add(int a, int b) // a and b are formal parameters
{
int c; // local variable
c=a+b;
return c;
}