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: a is 10, and b is 20.

  • Inside swapByValue: The values of x and y are swapped within the function, but these are just copies of a and b. The original variables a and b remain unchanged.

  • After the Function Call: a and b retain 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: a is 10, and b is 20.

  • Inside swapByReference: The pointers x and y hold the addresses of a and b, respectively. The values of a and b are swapped through these pointers, directly modifying the original variables.

  • After the Function Call: a and b have been swapped successfully, with a now being 20 and b being 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: a is 10, and b is 20.

  • Inside swapByReference: x and y are references to a and b, respectively. The swap operation directly modifies a and b.

  • After the Function Call: a and b are swapped successfully, with a now being 20 and b being 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