1// function definition to swap the values.
2void swap(int &x, int &y) {
3 int temp;
4 temp = x; /* save the value at address x */
5 x = y; /* put y into x */
6 y = temp; /* put x into y */
7
8 return;
9}
1#include <iostream>
2using namespace std;
3
4// function declaration
5void swap(int &x, int &y);
6
7int main () {
8 // local variable declaration:
9 int a = 100;
10 int b = 200;
11
12 cout << "Before swap, value of a :" << a << endl;
13 cout << "Before swap, value of b :" << b << endl;
14
15 /* calling a function to swap the values using variable reference.*/
16 swap(a, b);
17
18 cout << "After swap, value of a :" << a << endl;
19 cout << "After swap, value of b :" << b << endl;
20
21 return 0;
22}
1Value of i : 5
2Value of i reference : 5
3Value of d : 11.7
4Value of d reference : 11.7
5
1#include <iostream>
2
3using namespace std;
4
5int main () {
6 // declare simple variables
7 int i;
8 double d;
9
10 // declare reference variables
11 int& r = i;
12 double& s = d;
13
14 i = 5;
15 cout << "Value of i : " << i << endl;
16 cout << "Value of i reference : " << r << endl;
17
18 d = 11.7;
19 cout << "Value of d : " << d << endl;
20 cout << "Value of d reference : " << s << endl;
21
22 return 0;
23}