Write a C++ program to demonstrates call by reference functionality in user defined function.
Program Code:
#include<iostream>
using namespace std;
void swap(int &, int &);
int main()
{
int a, b;
cout<<"Enter any two number to swap with each other : ";
cin>>a;
cin>>b;
cout<<endl;
cout<<"Value of entered number before swaping : "<<endl;
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
swap(a, b);
cout<<"Value of entered number after swaping :"<<endl;
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
return 0;
}
void swap(int &x, int &y)
{
int temp;
temp = x;
x = y;
y = temp;
cout<<"Value of entered number within swaping function :"<<endl;
cout<<"a = "<<x<<endl;
cout<<"b= "<<y<<endl;
}
The program code is tested on www.jdoodle.com
Output:
Enter any two number to swap with each other : 10 15
Value of entered number before swaping :
a = 10
b = 15
Value of entered number within swaping function :
a = 15
b= 10
Value of entered number after swaping :
a = 15
b = 10
Thanks
Mukesh Rajput
Post A Comment:
0 comments: