Return to Examples
C++ Program:
The address of list[i] is computed at the time of the call and does not change after that.
#include <iostream.h>

void swap1(int &a, int &b)
{
  int temp = a;

  cout<<" Beginning of subprogram\n";
  cout<<"a= " << a <<endl;
  cout<<"b= " << b <<endl;
  a = b;
  b = temp;
  cout<<" End of subprogram\n";
  cout<<"a= " << a <<endl;
  cout<<"b= " << b <<endl;
}

void main()
{
 int i=3;
 int list[5]={ 2, 0, 4, 1, 3};

 cout<<"Before call  \n";
 cout<<"i= " << i <<endl;
 cout<<"list[i]= "<<list[i]<<endl;
 for(int j=0; j<5; j++)
    cout<<list[j]<<" ";
 cout<<endl;
 swap1(i, list[i]);
 cout<<"After call \n";
 cout<<"i= " << i <<endl;
 cout<<"list[i]= "<<list[i]<<endl;
 for(int k=0; k<5; k++)
    cout<<list[k]<<" ";
 cout<<endl;
}
The output will be
Before call
i= 3
list[i]= 1
2 0 4 1 3
 Beginning of subprogram
a= 3
b= 1
 End of subprogram
a= 1
b= 3
After call
i= 1
list[i]= 0
2 0 4 3 3
The parameter of b in subprogram was referenced to the box of list[3] at the time when the main program called the subprogram.  After that, the reference to which box is not changed.  Therefore, the element in list[3] is changed to the new value of b.