Swapping two variables
Let's delve deeper into the concepts of Call by Value and Call by Reference in C++ by using the example of swapping two numbers. This example is perfect for illustrating the differences between the two methods because the desired outcome (swapping the values of two variables) can only be achieved correctly using Call by Reference.
1. Swapping Two Numbers Using Call by Value
In Call by Value, a copy of each argument is passed to the function. Therefore, any modifications made to the arguments inside the function do not affect the original variables. Let's see this in action.
Example: Call by Value
#include <iostream>
// Function to swap two numbers (ineffective with Call by Value)
void swapByValue(int x, int y) {
int temp = x;
x = y;
y = temp;
// Values are swapped only within the scope of this function
std::cout << "Inside swapByValue function: x = " << x << ", y = " << y << std::endl;
}
int main() {
int a = 10;
int b = 20;
std::cout << "Before swapByValue: a = " << a << ", b = " << b << std::endl;
swapByValue(a, b); // Passing a and b by value
std::cout << "After swapByValue: a = " << a << ", b = " << b << std::endl;
return 0;
}Output:
Explanation:
Before the Function Call:
ais 10, andbis 20.Inside
swapByValue: The values ofxandyare swapped within the function, but these are just copies ofaandb. The original variablesaandbremain unchanged.After the Function Call:
aandbretain their original values because the function modified only the copies, not the actual variables.
2. Swapping Two Numbers Using Call by Reference
In Call by Reference, the actual memory addresses of the arguments are passed to the function. This allows the function to modify the original variables directly.
Example: Call by Reference Using Pointers
Output:
Explanation:
Before the Function Call:
ais 10, andbis 20.Inside
swapByReference: The pointersxandyhold the addresses ofaandb, respectively. The values ofaandbare swapped through these pointers, directly modifying the original variables.After the Function Call:
aandbhave been swapped successfully, withanow being 20 andbbeing 10.
3. Swapping Two Numbers Using Call by Reference (Alternative with References)
C++ also provides a more straightforward syntax for Call by Reference using reference variables. A reference is an alias for another variable, allowing direct manipulation of the original variable.
Example: Call by Reference Using References
Output:
Explanation:
Before the Function Call:
ais 10, andbis 20.Inside
swapByReference:xandyare references toaandb, respectively. The swap operation directly modifiesaandb.After the Function Call:
aandbare swapped successfully, withanow being 20 andbbeing 10.
Comparison: Call by Value vs. Call by Reference
Aspect
Call by Value
Call by Reference
Argument Passed
A copy of the value is passed to the function.
The address of the variable (or a reference) is passed.
Modification
The original variable remains unchanged.
The original variable can be modified directly.
Memory Usage
Requires additional memory to store the copy.
No additional memory is required; direct access.
Use Case
Use when you don’t want to modify the original data.
Use when you need to modify the original data.
Summary
Call by Value: The function receives a copy of the argument, so any modifications within the function do not affect the original variable. This method is safe but not effective for operations like swapping two numbers because it only swaps copies, not the originals.
Call by Reference: The function receives a reference to the argument (either as a pointer or a reference). Modifications within the function directly affect the original variable. This method is effective for operations like swapping two numbers because it swaps the actual values.
In the context of swapping two numbers, Call by Reference is the appropriate method because it allows the function to modify the original variables directly.
Last updated