Page 77 - Modular_V1.1_Flipbook
P. 77
For example:
void main()
{
display(21);
}
In the preceding line, we have provided the value for the age parameter in the display( ) function.
Let us merge all the codes to create a running program.
#include<iostream.h>
#include<conio.h>
int display(int age); Output:
Your age is: 21 years.
void main()
{
display(21);
getch();
}
int display(int age)
{
cout<<”Your age is: “<< age<<” years.”;
return 0;
}
Program 1: To calculate the addition, subtraction, multiplication, division and modulus of two
numbers using functions.
#include<iostream.h>
#include<conio.h>
int sum(int, int);
int sub(int, int);
int multi(int, int);
int div(int, int);
int mod(int, int);
void main()
{
int n1, n2;
cout<<”Enter first number: “;
cin>>n1;
cout<<”Enter second number: “;
cin>>n2;
cout<<”The sum of two numbers is: “<<sum(n1, n2)<<”\n”;
cout<<”The difference of two numbers is: “<<sub(n1, n2)<<”\n”;
Functions in C++ 75

