Types of function definitions
Defining a function is depends upon the logic in developer’s mind. We also understood a function can accept zero to many number of arguments and can returns zero to one return value. Based on this we can say, a function can be defined in four ways.
- No arguments and No return value
- No arguments but with return value
- with arguments and no return value
- with arguments and with return value
//Example of function with No arguments and No return value
#include
void add(void) //definition of the function
{
int x,y,sum;
printf(“Enter any two numbers:”);
scanf(“%d%d”,&x,&y);
sum=x+y;
printf(“sum of your numbers is :%d”,sum);
}
void main()
{
add(); //invocation of function
}
//Example of function with No arguments but with return value
#include
int add(void) //definition of the function
{
int x,y;
printf(“Enter any two numbers:”);
scanf(“%d%d”,&x,&y);
return(x+y);
}
void main()
{
int sum;
sum=add(); //invocation of function
printf(“sum of your numbers is :%d”,sum);
}
//Example of function with arguments but no return value
#include
void add(int x,int y) //definition of the function
{
printf(“sum of your numbers is :%d”,x+y);
}
void main()
{
int a,b;
printf(“Enter any two numbers:”);
scanf(“%d%d”,&a,&b);
add(a,b); //invocation of function
}
//Example of function with arguments and with return value
#include
int add(int x,int y) //definition of the function
{
return x+y;
}
void main()
{
int a,b;
printf(“Enter any two numbers:”);
scanf(“%d%d”,&a,&b);
printf(“sum of your numbers is %d”,add(a,b));
}