Solution:NIIT/GNIIT Sonugiri0032@gmail.com

Wednesday, January 06, 2016

Function call by reference in C++

Function call by reference in C++


Structure of the Problem Requirements

There are two types of function calling , call by values and call by reference. In function call by reference we declare the function parameters by reference instead of normal variables. Inside the function ,the reference is used to access the actual parameters used in call.In this example we swap the two integers with call by reference function.

Source Code

#include<iostream>
using namespace std;
void swap (int &a , int &b);
int main ()
{
 int num1,num2;
 char c;
 cout<<"\t \t \t LEP Tutorials \n ";
 cout<<" Enter Your 1st Number : ";
 cin>>num1;
 cout<<"  Enter Your 2nd Number : ";
 cin>>num2;
 cout<<"\t  Do You Want to Swap these Values ? Y/N ";
 cin>>c;
 if ( c =='y' || c== 'Y')
 {
  swap (num1,num2);
  cout<<" After Swap 1st Number :"<<num1;
  cout<<endl;
  cout<<" After Swap 2nd Number :"<<num2;
  cout<<endl;
 }
 else
 {
  return 0;
 }
}
void swap (int &a, int &b)
{
 int temp ;
 temp = a;
 a = b;
 b = temp;
}

Output of the Program

Function call by reference in C++
Share:

0 comments:

GNIITSOLUTION GNIIT SOLUTION. Powered by Blogger.

Translate

Blog Archive

Unordered List