notes_on_function_XI
notes_on_function_XI
Examples
1. Complete program using function to illustrate use of function in a
program.
#include<stdio.h>
#include<conio.h>
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
a. In the above example the following code is the function declaration code.
Exmplae 2
#include<stdio.h>
#include<conio.h>
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();
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>
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();
The above examples illustrates that a function can be written in different ways.
Example 4
#include<stdio.h>
#include<conio.h>
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
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();
Example 6
#include<stdio.h>
#include<conio.h>
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();
Quotient is 3
Example 7
#include<stdio.h>
#include<conio.h>
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();
Remainder is 1