Call by Reference in C++


Call by Reference is a mechanism in C++ in which we intend to modify the parameters that we pass on to our function permanently. The actual parameter values are automatically changed when we change the formal parameter values. Also note that the actual and formal parameters may or may not have the same name. But they must have the same address which connects them together. 

We may also understand the meaning of Formal and Actual parameters. Actual Parameters are the ones passed on to the function(c and d in the program below). What usually happens is that they are copied into the Formal Parameters(&i and &j in the program below) before they are worked with. These Formal Parameters are used inside the function to calculate values or provide other functionality. 

But in case of Reference Type variables being passed, the Formal and Actual parameters happen to be the exact same variables since they have the same address in memory. These variables are of pointer type i.e. those pointers that point to some other primitive variables such as Integers, Boolean, String etc or even a function at a fixed memory location.


In the program below, the actual parameters are a and b in the function swap(int &i,&j), which leaves &i and &j being the Formal parameters. This program swaps the two variable values being passed in the parameter. 


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

void swap(int &i, int &j);
int main()
{
      clrscr();
      int a, b, c, d;
      a = 1;
      b = 2;
      c = 3;
      d = 4;
      cout << "a and b: " << a << " " << b << "\n";
      swap(a, b); // no & operator needed
      cout << "a and b: " << a << " " << b << "\n";
      cout << "c and d: " << c << " " << d << "\n";
      swap(c, d); // c , d are Actual Parameters
      cout << "c and d: " << c << " " << d << "\n";
      getch();
      return 0;
}

void swap(int &i, int &j) // &i , &j are Formal Parameters
{
      int t;
      t = i; // no * operator needed
      i = j;     
      j = t;
}

Below you can see another program that uses the call by reference model to negate a number. We first make a class with one variable that it holds and a function to negate it. The function to negate the variable uses the address of the class object in action. It takes parameter as the address of the object and then negates its "i" variable. See code below:

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

class Negate
{
      int id;  // Private Variable
      public:
      int i;  // Public Variable
      cl(int i); // Constructor Defined
      ~cl();     // Destructor Defined
      
      void func_neg(Negate &o) 
     { 
          o.i = -o.i; // no temporary created
      } 
};
      
Negate::Negate(int num)
{
      cout << "Constructing " << num << "\n";
      id = num;
}

Negate::~Negate()

{
      cout << "Destructing " << id << "\n";
}

int main()
{
      clrscr();
      Negate o(1);
      o.i = 10;
      o.func_neg(o);
      cout << o.i << "\n";
      getch();
      return 0;
}

No comments:

Post a Comment

Custom Search