User-defined function as the name appears –“A function defined by a user/programmer”. these functions are not standard and these are the project-specific functions written by a programmer to make program modular and less complex.
There are three component in user-defined function –
— Function prototype declaration
— Function calling
— Function definition
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include<stdio.h> function Prototype Declaration; int main() { function1 calling; function2 calling; return 0; } function1 definition { ; } function2 definition { ; } |
A function must be declared prior to its calling.The function declaration is an information for the compiler about the existence of a function in a program.
Syntax
return_type function_name(input1_dataType,input2_dataType);
return_type = function output data type e.g int ,char,float,double etc.
input1_dataType , input2_dataType = Function’s arguments data type int ,char,float,double etc.
A function can be called from anywhere in the program with proper arguments as declared in the prototype.
Syntax
function_name(input1,input2);
Note: Type of data must be same as declared in the prototype of function for correct output.
A function definition has the set of statements under its name to perform any specific task.
Syntax
return_type function_name(dataType Input1,dataType Input2)
{
statement1;
statement2;
}
Write a function to perform addition of two integers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <stdio.h> int additiion(int x, int y); /*function prototype declaration*/ int main() { long int num1, num2; printf("\nEnter Num1 :"); scanf("%d",&num1); printf("\nEnter Num2 :"); scanf("%d",&num1); sum=addition(num1,num2); /*calling*/ printf("\nsum =%d",sum); return 0; } int additiion(int x, int y) /*function defintion*/ { int sum; sum=x+y; return(sum); } |
1 2 3 4 5 |
Output: Enter num1 : 45 Enter num1 : 95 sum =140 |