Prog 2 - Call by Address and Refernce
Prog 2 - Call by Address and Refernce
– 2
mn#include "stdafx.h"
#include<iostream>
void main()
int i,j;
i=10; j=20;
int temp=*x;
*x=*y;
*y=temp;
Call By Reference
To illustrate reference parameters in actual use—and to fully demonstrate their benefits—the swap( )
function is rewritten using references in the following program. Look carefully at how swap( ) is
declared and called.
#include <iostream>
using namespace std;
void swap(int &x, int &y); // Declare swap() using reference parameters.
int main()
{
int i, j;
i = 10;
j = 20;
cout << "Initial values of i and j: ";
cout << i << ' ' << j << '\n';
swap(j, i);
cout << "Swapped values of i and j: ";
cout << i << ' ' << j << '\n';
return 0;
}