#include
#include
void add();
void main()
{
add();
}
void add()
{
int a,b,c;
printf("Enter two numbers=");
scanf("%d%d",&a,&b);
c=a+b;
printf("\n Addition=%d",c);
}
Function with argument and no return type:
#include
#include
void main()
{
int a,b;
printf("Enter two number=");
scanf("%d%d",&a&b);
add(a,b);
}
void add(int x,int y)
{
int c;
c=x+y;
printf("sum=%d",c);
}
Function with no argument and return type:
#include
#include
int add();
void main()
{
printf("sum=%d",add());
}
int add()
{
int a,b,c;
printf("Enter two values=");
scanf("%d%d"&a&b);
c=a+b;
return(c);
}
Function with argument and return type:
#include
#include
int add(int,int);
void main()
{
int a,b,c;
printf("Enter value=");
scanf("%d%d",&a&b);
c=add(a,b);
printf("sum=%d",c);
}
int add(int x,int y)
{
return(x+y);
}
- It avoids rewriting of the same code.
- Seperating code into modular function which makes the program easier to design and understand.
- Passing values between function.
- The machanism is used to pass information to the function is the 'argument'.
The argument are also called as parameter.
types of argument :
- Actual argument:-
These are source of information, calling programs pass actual argument to called function.
- Formal argument:-
(i.e parameter)
The called function access the information using corrosponding formal argument.
program for function
void main()
{
int a,b;
printf("Enter two values=");
scanf("%d%d",&a&b); //here a and b are actual argument
add(a,b);
}
void add(int x,int y) //here x and y are formal argument
{
printf\n("addition=%d",x+y);
}
o/p=Enter two values= 2 3
addition=5