Understanding Pass by Reference in C++

Filed Under: C++
Understanding Pass By Reference

Introduction

In this tutorial, we are going to discuss the concept of pass by reference in C++.

Passing Arguments to a Function in C++

We can pass objects or variables by any of the two methods, namely, pass by value and pass by reference. Both work on different mechanisms and have their own pros and cons.

Let us have a deeper look at both of the above-mentioned techniques individually.

Pass by Value in C++

In pass by value method, the called function creates a whole new set of variables and copies the values of the arguments. Hence, it does not have access to the original variables and can only work on the copies made earlier.

Memory Status For Pass By Value
Memory Status For Pass By Value

The above block diagram depicts the working of the method for the example below.

#include <iostream>
using namespace std;

void my_swapper(int x, int y)
{
	int temp;
	temp=x;
	x=y;
	y=temp;
	//swapping takes place among x & y
	//and not in a &amp; b!
	cout<<"Swapped values are: "; 
	cout<<"a="<<x<<"  b="<<y<<endl;
}
int main()
{
	int a= 10, b= 20;
	cout<<"Original Values: ";
	cout<<"a="<<a<<"  b="<<b<<endl; //original values
	
	my_swapper(a,b); //pass by value
	
	cout<<"After swap: ";
	cout<<"a="<<a<<"  b="<<b<<endl; 
	//pass by value causes no change in original values
	
	return 0;
}

Output:

Original Values: a=10  b=20
Swapped values are: a=20  b=10
After swap: a=10  b=20

Here, x and y are the copies which store the values of the variables a and b respectively. As we can see the function my_swapper() only swaps the values among the copies and cannot change the original variables.

Pass by value is useful when the original values are not to be modified and ensures that the function cannot harm the original value.

Pass by Reference in C++

The method to pass by reference in C++ uses a different mechanism. Instead of passing values to a function being called, a reference to the original variable is created or passed. A reference is an alias for a pre-defined variable. That is, the same variable can be accessed using any of the variable or reference name.

Now let us look at an example to understand the same.

Memory Status For Pass By Reference
Memory Status For Pass By Reference

Similarly, the above block diagram depicts the working of the method for the example below.

#include <iostream>
using namespace std;

void my_swapper(int &x, int &amp;y) //references to variables created
{
	int temp;
	temp=x;
	x=y;
	y=temp;
	//swapping takes place and is reflected in original values
	
	cout<<"Swapped values are: "; 
	cout<<"a="<<x<<"  b="<<y<<endl;
}
int main()
{
	int a=10, b=20;
	cout<<"Original Values: ";
	cout<<"a="<<a<<"  b="<<b<<endl; //original values
	
	my_swapper(a,b); //pass by reference
	
	cout<<"After swap: ";
	cout<<"a="<<a<<"  b="<<b<<endl; 
	//pass by reference causes change in original values
	
	return 0;
}

Output:

Original Values: a=10  b=20
Swapped values are: a=20  b=10
After swap: a=20  b=10

Here, a and b are passed to the my_swapper() function. Hence, x and y become the reference variables which are alias to a and b respectively.

In this way swapping x and y practically affect the variables a and b. As we can see from the above output, this time a and b are actually swapped using their references. This also brings us to the concept of pointers in C++ which we covered earlier.

Example – Converting distance in feet or inches using Pass by Reference in C++

Further, let us look at an example to understand the use of the pass by reference method in C++.

#include <iostream>
using namespace std;

void convert(float &, char &amp;,char &);//function prototype

int main()
{
	//variable initialisation
	float distance;
	char choice, type='F';
	
	cout<<"Enter Distance in feet: ";
	cin>>distance;
	cout<<"\nYou want distance in feet or inches? (F/I): ";
	cin>>choice;
	
	switch(choice)
	{
		case 'F': convert(distance,type,choice);
					break;
		case 'I': convert(distance,type,choice);
					break;
					
		default: cout<<"\n\nError!!!"; //if wrong choice is entered
					exit(0);
	}
	cout<<"\nDistance = "<<distance<<" "<<type;
	return 0;
}
//pass by reference
void convert(float &d, char &amp;t, char &ch)
{
	switch(ch)
	{
		case 'F': if(t=='I')
		{
			d=d/12;
			t='F';
		}
		break;
		case 'I': if(t=='F')
		{
			d=d*12;
			t='I';
		}
		break;
	}
}

Output:

Output
Example – Output

In the above code, we convert distance in feet to feet or inches. Similar to the previous example this time inside the function convert(), d, t and ch are references to the variables distance, type and choice respectively. Hence, change in any of the references updates the original values too.

Conclusion

So in this tutorial, we learned the concept of pass by reference in C++, its working mechenism and use.

For any further questions, feel free to use the comments below.

References

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages