Simple example of Function in C++, which explain the working of call by reference:
Program Code:
#include <iostream>
using namespace std;
void swap(int &x, int &y);
int main ()
{
int a = 100;
int b = 200;
cout << "Before swap, value of a :" << a << endl;
cout << "Before swap, value of b :" << b << endl;
swap(a, b);
cout << "After swap, value of a :" << a << endl;
cout << "After swap, value of b :" << b << endl;
return 0;
}
void swap(int &x, int &y)
{
int temp;
temp = x;
x = y;
y = temp;
return;
}
The output of the program is tested on www.jdoodle.com
Output:
Before swap, value of a :100
Before swap, value of b :200
After swap, value of a :200
After swap, value of b :100
Thanks
Mukesh Rajput
Post A Comment:
0 comments: