/*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. */
}
Function prototype(declaration):
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.
Syntax of function prototype
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:
- name of the function is
add()
- return type of the function is
int.
- two arguments of type
int are passed to function.
Function prototype are not needed if user-definition function is written before
main() function.
Function call
Control of the program cannot be transferred to user-defined function unless it is called invoked.
Syntax of function call
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
Function definition contains programming codes to perform specific task.
Syntax of function definition
return_type function_name(type(1) argument(1),..,type(n) argument(n))
{
//body of function
}
Function definition has two major components:
0 comments:
Post a Comment