Pass by Reference / Value in C++
COS1511 - Introduction To Programming I · Learning C++
Pass by Reference / Value in C++
In C++, when you pass variables to functions, you can use two main methods: pass by value and pass by reference. Understanding these methods is essential for managing how data is handled in your programs.
Pass by Value
When you pass a variable by value, you send a copy of the variable to the function. This means that any changes made to the variable inside the function do not affect the original variable outside the function.
Here is an example:
void increment(int num) { // Function that increments the value by 1
num++; // Increment the copy of the variable
}
int main() {
int x = 5;
increment(x); // Pass x by value
// x is still 5 here
return 0;
}In this example, the function increment receives a copy of x. The increment operation affects only the copy, so the original x remains unchanged.
Remember: Use pass by value when you want to protect the original variable from changes inside the function.
Pass by Reference
When you pass a variable by reference, you send a reference (or address) of the variable to the function. This means that any changes made to the variable inside the function will affect the original variable outside the function.
Here is an example:
void increment(int &num) { // Function that increments the value by 1
num++; // Increment the original variable
}
int main() {
int x = 5;
increment(x); // Pass x by reference
// x is now 6 here
return 0;
}In this example, the function increment receives a reference to x. The increment operation affects the original variable, so x is now 6 after the function call.
Remember: Use pass by reference when you want to modify the original variable inside the function.
Comparison of Pass by Value and Pass by Reference
Here is a summary of the key differences between pass by value and pass by reference:
- Pass by Value: A copy of the variable is passed. Changes do not affect the original variable.
- Pass by Reference: A reference to the variable is passed. Changes affect the original variable.
- Memory Usage: Pass by value uses more memory if the data type is large, as it creates a copy. Pass by reference is more efficient for large data types.
- Function Signature: Pass by value uses the type of the variable, while pass by reference uses the type followed by an ampersand (
&).
When to Use Each Method
Choosing between pass by value and pass by reference depends on your needs:
- Use pass by value for small data types (like integers or characters) where performance is not a concern.
- Use pass by reference for large data types (like arrays or objects) to avoid unnecessary copying.
- Use pass by reference when you need to modify the original variable.
Watch out: Be careful when using pass by reference. Changes made inside the function will affect the original variable, which may not be what you want.
Function Overloading with Pass by Reference and Value
C++ allows function overloading, which means you can have multiple functions with the same name but different parameter types. You can overload functions to accept parameters by value or by reference.
Here is an example:
void display(int num) { // Pass by value
std::cout << "Value: " << num << std::endl;
}
void display(int &num) { // Pass by reference
std::cout << "Reference: " << num << std::endl;
}
int main() {
int x = 10;
display(x); // Calls the pass by value function
display(x); // Calls the pass by reference function
return 0;
}In this example, there are two display functions. The first one accepts an integer by value, and the second one accepts it by reference. The correct function is called based on how the argument is passed.
Practical Example
Let us consider a practical example where we want to swap two numbers using both methods. First, we will use pass by value:
void swapByValue(int a, int b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int x = 1, y = 2;
swapByValue(x, y);
// x is still 1, y is still 2
return 0;
}Now, let us implement the same functionality using pass by reference:
void swapByReference(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int x = 1, y = 2;
swapByReference(x, y);
// x is now 2, y is now 1
return 0;
}In the first example, the swap function does not change the values of x and y because it uses pass by value. In the second example, the values are swapped successfully because it uses pass by reference.
Conclusion
Understanding how to pass parameters in C++ is crucial for writing effective functions. Pass by value protects the original variables, while pass by reference allows for modifications. Choose the appropriate method based on your requirements for memory usage and data manipulation.
Remember: Always consider the implications of modifying variables when using pass by reference.
Check your understanding
- What is the main difference between pass by value and pass by reference?
- When would you choose to use pass by reference instead of pass by value?
- How does function overloading work with pass by value and pass by reference?
- What happens to the original variable when passed by value?