Pass by Reference is a method of passing arguments to a function where the function receives a reference (alias) to the original variable instead of a copy. This allows the function to directly modify the original value stored in memory.
- No copy of the variable is created.
- Changes made inside the function affect the original variable.
- Improves performance when working with large objects because copying is avoided.
#include <iostream>
using namespace std;
void updateValue(int &num){
num = num + 10;
}
int main(){
int number = 20;
cout << "Before Function Call: " << number << endl;
updateValue(number);
cout << "After Function Call: " << number << endl;
return 0;
}
Output
Before Function Call: 20 After Function Call: 30
Explanation
- number contains the value 20.
- The function receives number as a reference using &.
- Any modification made to num directly affects number.
- After the function call, the original value becomes 30.
Syntax
returnType functionName(dataType ¶meter){
// Function body
}
#include <iostream>
using namespace std;
void func(int& x) {
x--;
}
int main() {
int a = 5;
cout << a << endl;
func(a);
cout << a;
}
Output
5 4
Explanation: The function func() receives an integer by reference. Since x refers to the original variable a, the statement x-- directly decreases a by 1. As a result, the value of a changes from 5 to 4, demonstrating that pass by reference allows a function to modify the original variable.
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.
#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;
}
Output
Before Swapping: x: 3 y: 7 After Swapping: x: 7 y: 3
Explanation: 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. |