This chapter is the continuation to the function Introduction chapter.
Write a C program to add two integers. Make a function add to add integers and display sum in main() function.
/*Program to demonstrate the working of user defined function*/
#include <stdio.h>
int add(int a, int b); //function prototype(declaration)
int main(){
int num1,num2,sum;
printf("Enters two number to add\n");
scanf("%d %d",&num1,&num2);
sum=add(num1,num2); //function call
printf("sum=%d",sum);
return 0;
}
int add(int a,int b) //function declarator
{
/* Start of function definition. */
int add;
add=a+b;
return add; //return statement of function
/* End of function definition. */
}
Every function in C programming should be declared before they are used. These type of declaration are also called function prototype. Function prototype gives compiler information about function name, type of arguments to be passed and return type.
return_type function_name(type(1) argument(1),....,type(n) argument(n));
In the above example,int add(int a, int b); is a function prototype which provides following information to the compiler:
add()int.int are passed to function.Function prototype are not needed if user-definition function is written before main() function.
Control of the program cannot be transferred to user-defined function unless it is called invoked).
function_name(argument(1),....argument(n));
In the above example, function call is made using statement add(num1,num2); from main(). This make the control of program jump from that statement to function definition and executes the codes inside that function.
Function definition contains programming codes to perform specific task.
return_type function_name(type(1) argument(1),..,type(n) argument(n))
{
//body of function
}
Function definition has two major components:
Function declarator is the first line of function definition. When a function is invoked from calling function, control of the program is transferred to function declarator or called function.
return_type function_name(type(1) argument(1),....,type(n) argument(n))
Syntax of function declaration and declarator are almost same except, there is no semicolon at the end of declarator and function declarator is followed by function body.
In above example, int add(int a,int b) in line 12 is a function declarator.
Function declarator is followed by body of function which is composed of statements.
In programming, argument/parameter is a piece of data(constant or variable) passed from a program to the function.
In above example two variable, num1 and num2 are passed to function during function call and these arguments are accepted by arguments a and b in function definition.

Arguments that are passed in function call and arguments that are accepted in function definition should have same data type. For example:
If argument num1 was of int type and num2 was of float type then, argument variable a should be of type int and b should be of type float,i.e., type of argument during function call and function definition should be same.
A function can be called with or without an argument.
Return statement is used for returning a value from function definition to calling function.
return (expression);
OR
return;
For example:
return; return a; return (a+b);
In above example, value of variable add in add() function is returned and that value is stored in variable sum in main() function. The data type of expression in return statement should also match the return type of function.

All right reserved to www.programiz.com