Here on this page, we will discuss function with switch statement in C++,
As we know we used Function to reduce program size in C++ see this Program creating a menu using switch Statement
In the below program, we call a function in every case statement to reducing code size of a Program.
In the program below, we have used three user-defined functions.
<preint get_sum();
int get_sub();
int get_mul();
In the program below, each case statement will perform a different task.
Here is the program below,
Example of function with switch statement in C++
#include<iostream>
#include<stdlib> // exit(0)
using namespace std;
int num1, num2, result; // global variable declaration
int get_sum();
int get_sub();
int get_mul();
int get_sum()
{
cout<<"Enter any two number: ";
cin>>num1>>num2;
result = num1 + num2;
return result;
}
int get_sub()
{
cout<<"Enter any two number: ";
cin>>num1>>num2;
result = num1 - num2;
return result;
}
int get_mul()
{
cout<<"Enter any two number: ";
cin>>num1>>num2;
result = num1 * num2;
return result;
}
int main()
{
int select;
cout<<"1.Addition\n";
cout<<"2.Subtraction\n";
cout<<"3.Multiply\n";
cout<<"0.Exit";
cout<<"\nEnter Your Choice: ";
cin>>select;
cout<<endl;
switch(select)
{
case 1:
get_sum();
cout<<"\nTotal: "<<result;
break;
case 2:
get_sub();
cout<<"\nSubtraction: "<<result;
break;
case 3:
get_mul();
cout<<"\nMultiply: "<<result;
break;
case 0: exit(0);
default:
cout<<"\ninvalid choice....";
}
return 0;
}
OUTPUT
1.Addition
2.Subtraction
3.Multiply
0.Exit
Enter Your choice: 1
Enter First Number: 5
Enter Second Number: 8
Total: 13
a good code always has fewer statements, so here is another one,
#include<iostream>
#include<stdlib> // exit(0)
using namespace std;
int get_num1();
int get_num2();
int result;
int get_num1()
{
int num1;
cout<<"\nEnter First Number: "; cin>>num1;
return num1;
}
int get_num2()
{
int num2;
cout<<"\nEnter Second Number: "; cin>>num2;
return num2;
}
int main()
{
int choice;
cout<<"1.Addition\n";
cout<<"2.Subtraction\n";
cout<<"3.Multiply\n";
cout<<"0.Exit";
cout<<"\nEnter Your Choice: "; cin>>choice;
switch(choice)
{
case 1:
result = get_num1()+get_num2();
cout<<"\nTotal: "<<result;
break;
case 2:
result = get_num1()-get_num2();
cout<<"\nSubtraction: "<<result;
break;
case 3:
result = get_num1()*get_num2();
cout<<"\nMultiply: "<<result;
break;
case 0:
exit(0);
default:
cout<<"\ninvalid choice....";
}
return 0;
}
OUTPUT
1.Addition
2.Subtraction
3.Multiply
0.Exit
Enter Your choice: 2
Enter First Number: 5
Enter Second Number: 8
Substrection: -3
Explanation
- In the program, we have declared two variables but both variables are declared inside different functions. i.e. variable num1 inside function get_num1() while variable num2 inside function get_num2()
- Both functions are called from each
case
statement. Which will be executed if the condition in the case statement will be true.
suppose user-entered first number 5 and second 8 and select option 2 so, - In the switch statement
case
2 , the condition is becoming true so it will be executed.
C++ Examples