by Dinesh Thakur

In addition to call by value, a function can also be called by reference. C++ provides two ways of passing arguments as reference to a function. The arguments can be passed either as a reference type or as a pointer type.

A reference is an alias or an alternate name for a variable. When an argument is passed by reference, an alias or a reference of the actual argument is passed to the formal argument. Unlike call by value method, call by reference does not create a copy of the actual argument and the called function works with the original values. This implies that any changes made to the variable in the function body are reflected in the calling function.

In call by reference, a reference to an argument is created by suffixing the reference operator or ampersand (&) to the data type of an argument in both the function definition and the declaration. Note that if the reference operator is omitted in either the function definition or the function declaration, the compiler raises an error. However, reference operator is not specified in the function call.

The syntax of declaring a function with a reference type parameter is

     return_type function_name (Type& identifier)

To understand the concept of call by reference, consider the function inc ( ) defined in

Example. If the arguments are passed by reference, the prototype of the function becomes

void inc(int&, int&);

The function definition of inc ( ) can be given as

void inc(int& a, int& b)

{

II function body is same as in Example 5

}

 

The output of the program is

Values before function call in main():

x ==-2 and y ==7

Values within function inc():

a== -1 and b ==8

Values after function call in main():

x== -1 and y ==8

In this example, a and b become aliases to the actual arguments x and y. The inc ()function then increments the formal arguments a and b, and the changes are reflected in the actual arguments x and y in main ().

The call by reference mechanism requires no overhead of creating copies of arguments in the function and hence, it is faster and reduces memory utilization. It is efficient to use call by reference mechanism when the argument occupies a lot of memory space and copying the argument is not feasible.

Arrays as Arguments: Since an array represents a complete block of memory, an entire array cannot be passed to a function. However, a reference to the array can be passed to the function to avoid copying a big size of data. This implies that the called function works with an original array, however, referring to it by a different name.

Though an array is passed as a reference, a reference operator or ampersand (&) is not used to denote a reference argument as the array name itself contains the address of the first element. The syntax of passing an array to a function is given here.

     return_type function_name (type identifier []);

It can also be written as

return_type function_name(type[]);

The syntax of a function call passing an array as an argument is

function_name (array_name) ;

To understand the concept of an array as an argument, consider this example.

 

Example : A program to sort the elements using bubble sort

#include<iostream>

using namespace std;

int i, j, temp;

void BubbleAsc(int [], int);

int main ()

const int max = 10;

int arr[max] = {22,5,67,98,45,32,101,99,73,10};

cout<<”The list before sorting is: \n";

for (i = 0; i < max; ++i)

cout<<arr[i]<<" ";

BubbleAsc (arr, max);

cout <<"\nThe sorted list in ascending order is: \n";

for (i = 0; i < max; i++)

cout<<arr[i]<<" ";

return 0;

}

void BubbleAsc(int num[], int max)

{

for (i = 0; i < (max - 1); i++)

{

For (j = 1; j < max; j++)

{

if (num[j] < num[j-1])

{

temp = num[j];

num[j] = num[j-1];

num[j-1] = temp;

}}}}

The output of the program is

The list before sorting is

22 5 67 98 45 32 101 99 73 10

The sorted list in ascending order is

5 10 22 32 45 67 73 98 99 101

In this example, a reference num to array arr of type int is passed to the function BubbleAsc ()and thus, the changes made to num in BubbleAsc ()are reflected (see output) in the array arr in main ( ).

In addition to a single dimensional array, a multi-dimensional array can also be passed to a function. The syntax of passing a multi-dimensional array to a function is return_type function_name (type[] [size] [size] ).;

To understand the concept of passing a two-dimensional array to a function, consider this example.

Example : A program to multiply matrix by a number

 

#include<iostream>

using namespace std;

int i, j;

void PrintMatrix(int[] [2]);

void Multiply(int[] [2] ,int);

int main ()

{

int arr[2] [2] ={10, 43, 32, 64};

int x;

cout<<"\nThe original Matrix is:";

PrintMatrix(arr) ;

Cout<<"\nEnter a number: ";

cin>>x;

Multiply (arr, x);

Cout<<"\nThe new Matrix is:";

PrintMatrix(arr) ;

return 0;

}

void ,PrintMatrix(int num[2] [2])

{

for(i=0;i<2;i++)

{

Cout<<endl;

for(j=0;j<2;j++)

cout<<num[i] [j]<<” “;

}}

 

void Multiply(int num[2] [2], int x)

{

For(i=0; i<2;i++)

{

for(j=0;j<2;j++)

{

num [i] [j] = num[i] [j]*x;

}

}

 

The output of the program is

The original Matrix is:

 

10 43

32    64

Enter a number: 5

The new Matrix iS:

 

50     215

160   320

 

In this example, the array arr is passed to function Multiply ().Then a call to printMatrix()is made, to print the elements of an array num on the screen. Note that since reference of the array is passed to the function, changes are made to the original values.

The const Reference: When an argument is passed by reference, the compiler accesses the argument from its original location. This speeds up the execution of the program but on the other hand, the called function can modify the value of the argument and hence, the original value would be lost. Thus, in order to have the efficiency of call by reference as well as to prevent the modification of the value of the passed argument, it can be passed as a constant reference. When doing this, the compiler accesses the argument from its original location and also ensures that the value of the argument stays intact.

The constant reference is declared by prefixing the const keyword to the data type of the argument in the function declaration and definition. For instance, the function definition of a function sq_area()defined in Example , accepting a const reference can be given as

int sq_area(const int& s)

{

s++;

return s*s;

 

}

Structure as Reference Arguments: The structure reference is passed by suffixing an ampersand (&) to the name of the structure in both the function declaration and the definition. To understand the concept of structure as reference arguments, consider the prototype of the function calc () in

Example , which Can be given as

void calc (details&) ;

The function definition can be given as

void calc(details& dd)

II function body is the same as in Ex

The output of the program is

Total amount in called function 1500

Total amount in main(): 1500

In this example, the structure variable d1 of structure details is passed by reference to function calc (). So, the modified value of the data member total of structure details is reflected in main () .