Functions – is a way of modularizing your program
Syntax of function declaration: return_type function_name(parameter list)
Function Call by Value
1.) Function without return type and without parameter list
Example : void function_name();
In this type of function declaration: Input, Process and Output is in the User-Defined
Function
2.) Function without return type and without parameter list
Example : void function_name(int num);
In this type of function declaration: Input is in main, Process and Output is in the User-
Defined Function
3.) Function with return type and without parameter list
Example : int function_name();
In this type of function declaration: Input and Process is in the User-Defined Function
and Output is in main.
4.) Function with return type and with parameter list
Example : int function_name(int num);
In this type of function declaration: Input is in main, Process is in the User-Defined
Function and Output is in main.
Sample Programming Problem:
1.) Create a C++ program that will prompt the user to input two numbers and output the
highest number entered.
//Solution 1 - Function without return type and without parameter list
#include <iostream>
using namespace std;
void highest(); //function declaration
int main()
{
highest(); //function call
system("pause");
return 0;
}
void highest() //function definition or function body
{
int num1, num2, high;
//input
cout<<"Input first number : ";
cin>>num1;
cout<<"Input second number : ";
cin>>num2;
//process
if(num1 > num2)
high = num1;
else
high = num2;
//output
cout<<"Highest = "<<high<<endl;
}
//Solution 2 - Function without return type and with parameter list
#include <iostream>
using namespace std;
void highest(int num1, int num2); //function declaration
int main()
{ int num1, num2;
//input
cout<<"Input first number : ";
cin>>num1;
cout<<"Input second number : ";
cin>>num2;
highest(num1, num2); //function call
system("pause");
return 0;
void highest(int num1, int num2) //function definition or function body
{ int high;
//process
if(num1 > num2)
high = num1;
else
high = num2;
//output
cout<<"Highest = "<<high<<endl;
//Solution 3 - Function with return type and without parameter list
#include <iostream>
using namespace std;
int highest(); //function declaration
int main()
{ int high;
high = highest(); //function call
//output
cout<<"Highest = "<<high<<endl;
return 0;
}
int highest() //function definition or function body
{
int num1, num2, high;
//input
cout<<"Input first number : ";
cin>>num1;
cout<<"Input second number : ";
cin>>num2;
//process
if(num1 > num2)
high = num1;
else
high = num2;
return high;
}
//Solution 4 - Function with return type and with parameter list
#include <iostream>
using namespace std;
int highest(int num1, int num2); //function declaration
int main()
{ int num1, num2, high;
//input
cout<<"Input first number : ";
cin>>num1;
cout<<"Input second number : ";
cin>>num2;
high = highest(num1, num2); //function call
//output
cout<<"Highest = "<<high<<endl;
return 0;
}
int highest(int num1, int num2) //function definition or function body
{
int high;
//process
if(num1 > num2)
high = num1;
else
high = num2;
return high;
}
Additional Example :
1.) Create a C++ program that will generate a random number in the range of -10 to 10.
Determine if the generated random number is negative or positive and even or odd.
Implement this using function.
#include <iostream> //cin and cout
#include <cctype> //cin and cout
#include <cstdlib> //rand and srand
#include <ctime> //time
using namespace std;
int RandomNumber();
void PositiveNegative(int randomNumber);
void EvenOdd(int randomNumber);
int main()
{
int randomNumber;
char tryagain;
do
{ randomNumber = RandomNumber();
cout<<endl<<"The computer generate this random number
"<<randomNumber<<endl;
PositiveNegative(randomNumber);
EvenOdd(randomNumber);
cout<<endl<<endl<<"Press [Y/y] to try again."<<endl;
cin>>tryagain;
tryagain = toupper(tryagain);
}while(tryagain == 'Y');
return 0;
}
int RandomNumber()
{
int randomNumber;
srand(time(0));
randomNumber = rand() % 21 - 10; //-10 to 10
return randomNumber;
}
void PositiveNegative(int randomNumber)
{
if(randomNumber < 0)
cout<<endl<<randomNumber<<" is negative"<<endl;
else
cout<<endl<<randomNumber<<" is positive"<<endl;
}
void EvenOdd(int randomNumber)
{
if(randomNumber % 2 == 0)
cout<<endl<<randomNumber<<" is even"<<endl;
else
cout<<endl<<randomNumber<<" is odd"<<endl;
}
2.) Create a C++ program that will ask user to input how many random numbers to display
in the range of 1 to 10 by pressing [S/s] it will compute and display the sum of all the
random numbers generated and by pressing [P/p] it will compute and display the
product of all the random numbers generated. Use function declaration with return
type.
#include <iostream> //cin and cout
#include <cctype> //cin and cout
#include <cstdlib> //rand and srand
#include <ctime> //time
using namespace std;
char Menu();
int inputHowMany();
int SumOfRandom(int howmany);
int ProductOfRandom(int howmany);
int main()
{
char choice, tryagain;
int howmany, sum, prod;
do
{ choice = Menu();
if(choice == 'S')
{
howmany = inputHowMany();
cout<<endl<<"The Computer will now generate the "<<howmany<<"
random Numbers in the range of 1 to 10."<<endl;
sum = SumOfRandom(howmany);
cout<<endl<<"Sum of all the random numbers generated =
"<<sum<<endl;
}
else if(choice == 'P')
{
howmany = inputHowMany();
cout<<endl<<"The Computer will now generate the "<<howmany<<"
random Numbers in the range of 1 to 10."<<endl;
prod = ProductOfRandom(howmany);
cout<<endl<<"Product of all the random numbers generated =
"<<prod<<endl;
}
cout<<endl<<"Press [Y/y] to try again"<<endl;
cin>>tryagain;
tryagain = toupper(tryagain);
}while(tryagain=='Y');
return 0;
}
char Menu()
{ char choice;
do{
cout<<endl<<"Press [S/s] to compute the sum of random numbers in the range of 1
to 10";
cout<<endl<<"Press [P/p] to compute the product of random numbers in the
range of 1 to 10";
cout<<endl<<"Press [X/x] to exit"<<endl;
cin>>choice;
choice = toupper(choice);
if(choice!='P' && choice!='S' && choice!='X')
cout<<endl<<choice<<" is INVALID! Please check the VALID option."<<endl;
}while(choice!='P' && choice!='S' && choice!='X');
return choice;
}
int inputHowMany()
{ int howmany;
do
{
cout<<endl<<"How many random numbers in the range 1-10 would you like
to generate (1-100)? ";
cin>>howmany;
if(howmany<1 || howmany>100)
cout<<endl<<howmany<<" is INVALID! 1-100 only."<<endl;
}while(howmany<1 || howmany>100);
return howmany;
}
int SumOfRandom(int howmany)
{ int randomNumber, sum, ctr;
srand(time(0));
for(sum=0, ctr=1; ctr<=howmany; ctr++)
{
randomNumber = rand() % 10 + 1;
cout<<randomNumber<<"\t";
sum += randomNumber;
}
return sum;
}
int ProductOfRandom(int howmany)
{ int randomNumber, prod, ctr;
srand(time(0));
for(prod=1, ctr=1; ctr<=howmany; ctr++)
{
randomNumber = rand() % 10 + 1;
cout<<randomNumber<<"\t";
prod *= randomNumber;
}
return prod;
}