OOPS - [C++ Reference Variable]

♠ Posted by Unknown in at 10:26

Reference Variable


When you use a reference parameter, the compiler automatically passes the address of the variable used as the argument. There is no need to manually generate the address of the argument by preceding it with an &. Further within the function, the compiler automatically uses the variable pointed to by the reference parameter. There is no need to employ the *. Thus a reference parameter fully automatically the call-by-reference parameter-passing mechanism.

#include<iostream.h>
#include<conio.h>

void swap(int &x, int &y);

void main()
{
 int i, j;
 clrscr();
 i = 10;
 j = 20;
 cout<<"Before Passing...."<<endl;
 cout<<"i : "<<i<<endl;
 cout<<"j : "<<j<<endl;
 swap(i, j);
 cout<<"After Passing....."<<endl;
 cout<<"i : "<<i<<endl;
 cout<<"j : "<<j<<endl;
 getch();
}
void swap(int &x, int &y)
{
      int z;
      z = x;
      x = y;
      y = z;
}

0 comments:

Post a Comment