User-Defined Functions
In a C program a programmer create a function to perform some specific task is called user-defined functions. User-defined functions has no type that are task specific which can be modified by the programmer.
For example if we need to calculate the sum of two number more than times them we can create a function which perform addition of two number several times in program hence creating a function is useful because it save our time space and it is also easy to understand whenever we need to perform addition of two number then we have to just call it and left the addition action on that function. When a function is called sometimes it takes some value that is arguments and returns the result to the calling function.
Lets check a simple demonstration of an user-defined function
Program: Write a program to find out sum of two numbers.
#include<stdio.h>
int main(void)
{
double num1, num2, sum;
printf("Enter any two numbers : ");
scanf("%d%d", &num1, &num2);
sum=addtion(num1, num2); //Function call
printf("Sum of %d and %d is : %d", num1, num2, sum);
return 0;
}
int addtion(int a, int b) //Function body or Function definition
{
int sum;
sum = a+b;
return sum; //Return the sum
}
Output
Enter any two number : 10 20
Sum of 10 and 20 is : 30
Function Definition
Function definition contain the whole code and description of a function. It describe the whole functionality of the function, what task is to be done by the function which parameters it has to be take and what type of result it will returns to the calling function.
Normally a function definition is combined into two parts
- A function header.
- Function body.
Following is the general syntax of function.
return_type func_name(parameters declaration)
{
local variable declaration;
statements;
--------------------------
--------------------------
return (expression);
}
In above code the first line is known as
function header, after function header next whole part is
function body which is written enclosed in curly braces.
The return_type means the type of value which will be returned by the function. A function can return either one value or no value. If the return type of function is no value means if a function that does not return any value then void should be written in place of return type. If no return type specified in the definition then int return type is assumed.
func_name specify the name of the function and it can be any valid C identifier. After the function name a comma separated list of parameters declaration is given inside parentheses which define the type and name of the parameter.These parameters are also called formal parameters and used to accept the values from the calling function.A function can have any number of parameters even no parameters at all.
The body of function is a block which consist of declarations of variables, and C statements followed by additional optional return statement. The variable which are declared inside the function is known as local variables which means the existence of that variable is only for that function. Local variable can not be accessed directly outside the function.
The function definition can be placed anywhere in the program or function definition can also be placed in different source file. But generally all definitions are placed after the main() function. A function definition can not be placed inside any function.