How to pass an array by reference in C++

In this post, we will understand about passing an array by reference in the C++ programming language. We will see the example code also for this task.

Passing an array by reference in C++

There are mainly two types of ways by which we can pass an array to a function for processing according to the user.

1.CALL BY VALUE.

2. CALL BY REFERENCE.

Call by value: In this method, we pass the array directly to the caller function. As a result of this in this function call, it copies the actual parameter to the formal parameters.

In C++ language, we can process calls by value by passing the name of the array variable as an actual argument whereas, in the call by reference, instead of passing the array we only pass the reference of the array i.e we pass the address of an array as the actual argument.

In the C++ language, we can either use pointers or references for pass by reference.

The main difference between passing an array using call by value and using call by reference is that in case of call by Value, any changes made to the formal argument does not affect the value of the actual argument the changes made to informal arguments does not get reflected in the actual argument whereas, in case of call by value, any changes made to the formal parameter get stored in the actual parameters.

Reason: The key reason behind this kind of behavior is that in case of call by value, an additional set of copies of the actual arguments gets formed, and then the processing or change gets stored on them whereas, with a call by reference, the reference to the actual argument gets past because of which extra set of copies do not get created and thus any changes made get stored permanently.

We will try to understand the working of call by value and call by reference using the following Example:

// CAll by value and Call by reference using C++

#include<iostream>
#include<conio.h>
using namespace std;
void change1(int);
void change2(int*);

int main()
{
  int X,Y,A,B;
  
  // assigning value to these variable
  X=1;
  Y=2;
  A=34;
  B=90;
  
  cout<<"Values Before Any processing :X="<<X<<",Y="<<Y<<",A="<<A<<",B="<<B<<endl;
  change2(&A);   //call by Reference 
  change2(&B);   // Call by Reference 
  cout<<"\n";
  cout<<"Values after processing :X="<<X<<",Y="<<Y<<",A="<<A<<",B="<<B<<endl;  /* Only in variable A and B the changes gets reflected                         Back..Because they are being passed using pass by reference.
                                                                      In OUTPUT : X=1,Y=2,A=34,B=90 */
getch();	// used to hold the output 
return 0;	
}

void change1(int var)
{
  var=100;	
}

void change2(int *var)  // *var is a pointer var which is used to store the address or reference of actual parameter A and B
{
  *var=100;
}

OUTPUT:

X=1,Y=2,A=100,B=100

Explanation: Since X and Y get called by a call by value so the changes made in them do not get reflected whereas, since A and B get sent using call by reference method, changes made in them in called function got reflected.

 

You may also visit:

 

Leave a Reply