C++ Functions - Pass By Reference
Last Updated :
26 May, 2025
In C++, there are different ways to pass data (or variables) to a function, with two common methods being Passing by Value and Passing by Reference. Passing by Reference lets a function modify a variable directly, without creating a copy. The variable and parameter share the same memory location, so any changes to the parameter affect the original variable.
What is a Pass by Reference?
When a variable is passed as a reference to a function, the address of the variable is stored in a reference variable inside the function. Hence, the variable inside the function is an alias for the passed variable. Therefore, any operations performed on the variable inside the function will also be reflected in the calling function.
- This ability to reflect changes could return more than one value by a function.
- Also, a void function could technically return values using this method.
The & (address of) operator denotes values passed by pass-by-reference in a function definition.
Syntax:
C++
int func(int& a, int& b){
// function body
}
In the above statement, parameters of the function are reference variables to achieve pass by reference.
Example:
C++
#include <iostream>
using namespace std;
void func(int& x) {
x--;
}
int main() {
int a = 5;
cout << a << endl;
func(a);
cout << a;
}
In this program, the function func takes an integer reference as a parameter. When the function is called with a as the argument, it modifies the value of a directly by decrementing it using the statement x--. Since x is a reference to a, any changes made to x also affect a. Therefore, the value of a is decreased by 1, from 5 to 4. This demonstrates how a function can modify the value of a variable passed by reference.
Swap function using Pass-By-Reference
The swap function swaps the values of the two variables it receives as arguments. Below is the C++ program to swap the values of two variables using pass-by-reference.
C++
#include <bits/stdc++.h>
using namespace std;
// Swap function
void swap(int &a, int &b) {
int temp;
temp = b;
b = a;
a = temp;
}
int main() {
int x = 3, y = 7;
// Before swapping
cout << "Before Swapping: "
<< endl;
cout << "x: " << x << " y: "
<< y << endl;
// Call the function
swap(x, y);
// After swapping
cout << "After Swapping: "
<< endl;
cout << "x: " << x << " y: "
<< y;
return 0;
}
OutputBefore Swapping:
x: 3 y: 7
After Swapping:
x: 7 y: 3
In this program, the swap function swaps the values of two variables by passing them as references. When the function is called, it modifies the values of a and b directly. When we print the value of x and y after calling swap function, x value is 7 and y value is 3.
Pass by Reference vs Pass by Value
The following table compares the key differences between Pass by Reference and Pass by value in C++:
Pass By Reference | Pass By Value |
---|
The actual reference (memory address) of the variable is passed. | A copy of the actual value is passed to the function. |
The original variable is modified. | The original variable is not modified. |
More memory efficient since no copy of the variable is made. | Requires more memory since a copy of the variable is created |
Changes inside the function affect the original variable. | Changes inside the function do not affect the original variable. |
Suitable when you want to modify the original data or avoid copying large data. | Suitable when you don’t want to alter the original data. |
Similar Reads
Pass By Reference In C Passing by reference is a technique for passing parameters to a function. It is also known as call by reference, call by pointers, and pass by pointers. In this article, we will discuss this technique and how to implement it in our C program. Pass By Reference in C In this method, the address of an
4 min read
Function Call by Reference in Objective-C Just like other languages in objective-C also a function is used to perform some specific task. In objective-C, we can call a function by value or by reference. So, in this article, we will talk about the call by reference in Objective-C. The call-by-reference is a process of passing arguments to a
3 min read
Pass Array to Functions in C++ In C++, a collection of elements stored in contiguous memory locations and having the same data type is called an array. Passing arrays to functions is done to perform various operations on array elements without messing up with the main code. In C++, an array can be passed in a function using a poi
5 min read
Perl | Pass By Reference When a variable is passed by reference function operates on original data in the function. Passing by reference allows the function to change the original value of a variable. When the values of the elements in the argument arrays @_ are changed, the values of the corresponding arguments will also c
2 min read
Pass Array to Functions in C Passing an array to a function allows the function to directly access and modify the original array. In this article, we will learn how to pass arrays to functions in C.In C, arrays are always passed to function as pointers. They cannot be passed by value because of the array decay due to which, whe
3 min read