Functions
Examples
1. Complete program using function to illustrate use of function in a
program.
#include<stdio.h>
#include<conio.h>
void add(int a, int b)
{
int sum;
sum=a+b;
printf("\n sum of two given number is %d",sum);
}
main()
{
int x, y;
clrscr();
printf("\n This program adds two numbers using function");
printf("\n Enter two numbers");
scanf("%d%d", &x,&y);
add(x,y);
getch();
OUTPUT
This program adds two numbers using function
Enter two numbers5 7
sum of two given number is 12
___________________________________________________________________________
a. In the above example the following code is the function declaration code.
void add(int a, int b)
{
int sum;
sum=a+b;
printf("\n sum of two given number is %d",sum);
}
b. Below given is the first line of the function
void add(int a, int b)
c. void is used when function does not return any data
d. add – is the name of the function
e. int a , int b - are called parameters or formal parameters .
f. Following are the executables statements which forms body of the function
int sum;
sum=a+b;
printf("\n sum of two given number is %d",sum);
They are enclosed in { }
g. The function is called by its name as follows
add(x,y);
Exmplae 2
#include<stdio.h>
#include<conio.h>
int add(int a, int b)
{
int sum;
sum=a+b;
return sum;
}
main()
{
int x, y;
clrscr();
printf("\n This program adds two numbers using function");
printf("\n Enter two numbers");
scanf("%d%d", &x,&y);
printf("\n sum of two given number is %d",add(x,y));
getch();
This program adds two numbers using function
Enter two numbers5 8
sum of two given number is 13
In this example the function returns the value using return statement. The function is called
also in different way in this example.
Example 3.
#include<stdio.h>
#include<conio.h>
int add(int a, int b)
{
return a+b;
}
main()
{
int x, y;
clrscr();
printf("\n This program adds two numbers using function");
printf("\n Enter two numbers");
scanf("%d%d", &x,&y);
printf("\n sum of two given number is %d",add(x,y));
getch();
This program adds two numbers using function
Enter two numbers 7 6
sum of two given number is 13
The above examples illustrates that a function can be written in different ways.
Example 4
#include<stdio.h>
#include<conio.h>
int sub(int a, int b)
{
return a - b;
}
main()
{
int x, y;
clrscr();
printf("\n This program finds the difference between two numbers using function");
printf("\n Enter two numbers");
scanf("%d%d", &x,&y);
printf("\n Difference between two given number is %d",sub(x,y));
getch();
}
This program finds the difference between two numbers using function
Enter two numbers 7 5
Difference between two given number is 2
Example 5
#include<stdio.h>
#include<conio.h>
int mul(int a, int b)
{
return a * b;
}
main()
{
int x, y;
clrscr();
printf("\n This program finds the product of two numbers using function");
printf("\n Enter two numbers");
scanf("%d%d", &x,&y);
printf("\n Product of two given number is %d",mul(x,y));
getch();
This program finds the product of two numbers using function
Enter two numbers 7 8
Product of two given number is 56
Example 6
#include<stdio.h>
#include<conio.h>
int div(int a, int b)
{
return a / b;
}
main()
{
int x, y;
clrscr();
printf("\n This program finds the quotient using function");
printf("\n Enter two numbers");
scanf("%d%d", &x,&y);
printf("\n Quotient is %d",div(x,y));
getch();
This program finds the quotient using function
Enter two numbers 7 2
Quotient is 3
Example 7
#include<stdio.h>
#include<conio.h>
int rem(int a, int b)
{
return a % b;
}
main()
{
int x, y;
clrscr();
printf("\n This program finds the remainder using function");
printf("\n Enter two numbers");
scanf("%d%d", &x,&y);
printf("\n Remainder is %d",rem(x,y));
getch();
This program finds the remainder using function
Enter two numbers 7 2
Remainder is 1