function exercise

Here in this page we will discuss following two examples,


 

switch with function

In the program below, we have used two user defined functions.

int get_num1();
int get_num2();

Both these functions will take one input from the user.

In the program, each case statement will perform a different task.

Here is the program below,

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>  // exit(0)

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;
 }


void main()
{
 clrscr();
 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<<"\nSubstraction: "<<result;
	 break;

  case 3:
	 result = get_num1()*get_num2();
	 cout<<"\nMultiply: "<<result;
	 break;

  case 0:
	 exit(0);

  default:
	  cout<<"\ninvalid choice....";
 }

getch();
}
OUTPUT:- 
1.Addition 
2.Subtraction 
3.Multiply 
0.Exit 

Enter Your choice: 2 
Enter First Number: 5 

Enter Second Number: 8 

Substraction: -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 is true.

suppose user entered first number 5 and second 8 and select option 2 so,

In the switch statement case 2 will be execute and will perform the task.


 

storing student detail and display them by other function

In following program we will store student detail like roll-number, age and name and displaying them with other function

We have used two user defined functions in the program.

In this, the function void get_data() will stores the details from the user while the void put_data() will print that detail.

In a way, we can say that the function void get_data() will act as input and function void put_data() will act as output.here is a diagram,

store-student-detail-using-function-in-cpp

here is the program,

#include
#include
#include // for gets() function

// data member declare in globally
int roll,age;
char name[10];

// function member declaration
void get_data();
void put_data();

void main(void)
{
   clrscr();

   getdata();
   putdata();

   getch();
}

void get_data()
{
   cout<<"\nEnter roll no: ";
   cin>>roll;
   cout<<"Enter age : ";
   cin>>age;
   cout<<"Enter Name : ";
   gets(name);
}

void put_data()
{
   cout<<"\nDisplay Record";
   cout<<"\nRoll no : "<<roll;
   cout<<"\nStu. age: "<<age;
   cout<<"\nStu.Name: "<<name;
}
OUTPUT:-
Enter roll no: 101
Enter age : 25
Enter Name : Rohit sherma

Display Record
Roll no : 101
Stu. age: 25
Stu.Name: Rohit sherma