User-defined functions can be categorized as:
1. Function with no arguments and no return value
2. Function with no arguments and return value
3. Function with arguments but no return value
4. Function with arguments and return value
1. Function with no arguments and no return value
#include<stdio.h>
#include<conio.h>
void sum( );
//Function Prototype/Declaration with no arguments and no return value
void main()
{
clrscr();
sum(x, y);
//calling of function-sum
getch();
}
//Definition of function sum
void sum( )
{
int x,y,res;
printf("Enter value of x and y: ");
scanf("%d %d", &x, &y);
res = a + b;
printf("Sum of %d + %d = %d",a,b,result);
}
|
2. Function with no arguments and return value
#include<stdio.h>
#include<conio.h>
int sum( ); //Function Prototype/Declaration with no arguments and return value
void main()
{
int result;
clrscr();
result=sum( );
//calling of function-sum
printf("Sum of x + y = %d", result);
getch();
}
//Definition of function sum
void sum( )
{
int x,y,res;
printf("Enter value of x and y: ");
scanf("%d %d", &x, &y);
res = a + b;
return res; //returning integer value
}
|
3. Function with arguments and no return value
#include<stdio.h>
#include<conio.h>
void sum(int , int);
//Function Prototype/Declaration with arguments and no return value
void main()
{
int x,y;
clrscr();
printf("Enter value of x and y: ");
scanf("%d %d", &x, &y);
sum(x, y);
//calling of function-sum
getch();
}
//Definition of function sum
void sum(int a, int b)
{
int res;
res = a + b;
printf("Sum of %d + %d = %d",a,b,result);
}
|
4. Function with arguments and return value
#include<stdio.h>
#include<conio.h>
int sum(int , int); //Function Prototype/Declaration with arguments and return value
void main()
{
int x,y,result;
clrscr();
printf("Enter value of x and y: ");
scanf("%d %d", &x, &y);
result=sum(x, y); //calling of function-sum
printf("Sum of %d + %d = %d",x,y,result);
getch();
}
//Definition of function sum
int sum(int a, int b)
{
int res;
res = a + b;
return res;
}
|
good
ReplyDeleteThanks Jens
Delete