Metadata
- 📅 Date :: 12-06-2025
- 🏷️ Tags :: cpp
Notes
Understanding Pass by Value and Pass by Reference in C++
In C++, when you pass arguments to a function, there are two main ways to do so:
- Pass by Value
- Pass by Reference
Let’s break down both concepts with a practical example and see how they work:
Pass by Value
In pass by value, the function receives a copy of the original variable. This means that the function operates on the copy and not on the original variable itself. Changes made to the parameter inside the function do not affect the original variable outside the function.
Example (Pass by Value):
Let’s say we have two variables, x and y, that contain different values (such as “kool-aid” and “water”). If we want to swap these two values using a function, we might first try this:
void swap(string x, string y) {
string temp = x;
x = y;
y = temp;
}
int main() {
string x = "kool-aid";
string y = "water";
swap(x, y);
// x is still "kool-aid" and y is still "water"
cout << "x: " << x << ", y: " << y << endl;
}
Here, x and y remain unchanged outside the swap function because we are passing copies of x and y to the function. The original variables in the main function were unaffected by the swap.
Why Does This Happen?
This happens because when you pass by value, the function receives copies of the original variables. Any changes made inside the function only affect those copies, and not the actual variables in memory. So when we try to swap x and y, we’re only swapping their copies, not the original variables.
Pass by Reference
In pass by reference, the function does not receive a copy of the variable. Instead, it receives the memory address (reference) of the variable. This allows the function to directly manipulate the original variable in memory.
Example (Pass by Reference):
To make our function work properly and actually swap the values of x and y, we can modify the function to use pass by reference:
void swap(string &x, string &y) {
string temp = x;
x = y;
y = temp;
}
int main() {
string x = "kool-aid";
string y = "water";
swap(x, y);
// x now contains "water" and y contains "kool-aid"
cout << "x: " << x << ", y: " << y << endl;
}
Here, the values of x and y are successfully swapped because we passed the memory addresses of x and y (using the address-of operator &) to the swap function. By using references, any changes inside the function affect the original variables.
Key Difference Between Pass by Value and Pass by Reference:
- Pass by Value:
- A copy of the original variable is passed to the function.
- Changes inside the function do not affect the original variable.
- The original variables outside the function remain unchanged.
- Pass by Reference:
- The memory address of the original variable is passed to the function.
- Changes made inside the function directly affect the original variable.
- The original variable’s value is updated after the function call.
Demonstrating with Memory Addresses
To reinforce the concept of pass by reference and pass by value, you can inspect the memory addresses of the variables. The address-of operator (&) in C++ returns the memory address of a variable.
Example (Inspecting Memory Addresses):
void swap(string &x, string &y) {
cout << "Address of x in function: " << &x << endl;
cout << "Address of y in function: " << &y << endl;
string temp = x;
x = y;
y = temp;
}
int main() {
string x = "kool-aid";
string y = "water";
cout << "Address of x in main: " << &x << endl;
cout << "Address of y in main: " << &y << endl;
swap(x, y);
}
Output:
Address of x in main: 0x7ffee59f5c90
Address of y in main: 0x7ffee59f5c60
Address of x in function: 0x7ffee59f5c90
Address of y in function: 0x7ffee59f5c60
- Before and after calling the
swapfunction, we see that the memory addresses ofxandyare the same in both the main function and theswapfunction, indicating that the function is using the original variables (not copies) when passed by reference.
Which to Use?
- Pass by Value: Use this when you do not want the function to modify the original variables. This is typically used for simple data types or when performance is not a concern.
- Pass by Reference: Use this when you need the function to modify the original variable. This is often used for large objects or when the function needs to change the value of the parameters.
Summary:
- Pass by Value: A copy of the variable is passed to the function. Changes to the copy inside the function do not affect the original variable.
- Pass by Reference: A reference (memory address) to the variable is passed. Changes to the variable inside the function directly affect the original variable.