Functions - Is A Way of Modularizing Your Program: //solution 1 - Function Without Return Type and Without Parameter List
Functions - Is A Way of Modularizing Your Program: //solution 1 - Function Without Return Type and Without Parameter List
In this type of function declaration: Input, Process and Output is in the User-Defined
Function
In this type of function declaration: Input is in main, Process and Output is in the User-
Defined Function
In this type of function declaration: Input and Process is in the User-Defined Function
and Output is in main.
1.) Create a C++ program that will prompt the user to input two numbers and output the
highest number entered.
int main()
{
highest(); //function call
system("pause");
return 0;
}
//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;
}
#include <iostream>
//input
cin>>num1;
cin>>num2;
system("pause");
return 0;
{ int high;
//process
high = num1;
else
high = num2;
//output
cout<<"Highest = "<<high<<endl;
#include <iostream>
using namespace std;
int main()
{ int high;
//output
cout<<"Highest = "<<high<<endl;
return 0;
}
//input
cout<<"Input first number : ";
cin>>num1;
cout<<"Input second number : ";
cin>>num2;
//process
if(num1 > num2)
high = num1;
else
high = num2;
return high;
}
#include <iostream>
using namespace std;
int main()
{ int num1, num2, high;
//input
cout<<"Input first number : ";
cin>>num1;
cout<<"Input second number : ";
cin>>num2;
//output
cout<<"Highest = "<<high<<endl;
return 0;
}
//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.
int RandomNumber();
void PositiveNegative(int randomNumber);
void EvenOdd(int randomNumber);
int main()
{
int randomNumber;
char tryagain;
do
{ randomNumber = RandomNumber();
return 0;
}
int RandomNumber()
{
int randomNumber;
srand(time(0));
return randomNumber;
}
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;
}
srand(time(0));
return sum;
}
srand(time(0));
return prod;
}