Pass-by-Value vs. Pass-by-Reference in C++ Function Parameters

In C++, a function is like a “processing machine” where we need to input some “raw materials” (i.e., parameters), and the machine outputs the result after processing. The way parameters are passed directly affects how the “raw materials” change inside and outside the function. The two most common passing methods are pass-by-value and pass-by-reference. Today, we’ll explore their differences in detail.

一、Pass-by-Value: Copy the “Raw Material” to the Function

Pass-by-value is the most basic parameter passing method. When using pass-by-value, the function’s formal parameter (the parameter defined in the function) receives a copy of the actual parameter (the parameter passed when calling the function). This means the function operates on this copy and will not affect the original actual parameter.

Example: Swapping the values of two variables (using pass-by-value)

// Define a function to swap two int variables using pass-by-value
void swap_value(int a, int b) {
    int temp = a;  // Temporarily store the value of a
    a = b;         // Modify the formal parameter a (copy)
    b = temp;      // Modify the formal parameter b (copy)
}

int main() {
    int x = 10, y = 20;
    swap_value(x, y);  // Call the function, passing x and y
    // Are the values of x and y still 10 and 20?
    // Yes! Because swap_value modifies the "copy" of x and y, not the original variables
    return 0;
}

Characteristics of Pass-by-Value:
- A copy of the actual parameter’s value is passed; the formal parameter and actual parameter are two independent variables.
- Modifying the formal parameter inside the function does not affect the external actual parameter.
- Suitable for scenarios where the original data does not need to be modified (e.g., calculating the square of a number).

二、Pass-by-Reference: Directly “Borrow” the Original Variable

Pass-by-reference is a C++-specific method that uses a reference type (& symbol) as the parameter. A reference is essentially an “alias” for a variable and directly points to the memory address of the actual parameter. Thus, operating on the formal parameter inside the function is equivalent to directly operating on the actual parameter.

Example: Swapping the values of two variables (using pass-by-reference)

// Define a function to swap two int variables using pass-by-reference
void swap_reference(int &a, int &b) {  // Formal parameters are reference types
    int temp = a;  // Temporarily store the value of a
    a = b;         // Modify the formal parameter a (original variable x)
    b = temp;      // Modify the formal parameter b (original variable y)
}

int main() {
    int x = 10, y = 20;
    swap_reference(x, y);  // Call the function, passing x and y
    // Now x and y become 20 and 10! Because a and b are references to x and y, modifying them changes the original variables
    return 0;
}

Characteristics of Pass-by-Reference:
- A reference to the actual parameter is passed (essentially pointing to the original variable’s address); the formal parameter and actual parameter are the same variable.
- Modifying the formal parameter inside the function directly affects the external actual parameter.
- Suitable for scenarios where the original data needs to be modified or large objects are passed to avoid copy overhead (e.g., arrays, large structs).

三、Pass-by-Value vs Pass-by-Reference: Core Differences

Comparison Item Pass-by-Value Pass-by-Reference
What is Passed A “copy” of the actual parameter’s value A “reference” to the actual parameter (directly points to the original variable)
Effect of Modification Modifying the formal parameter inside the function does not affect the actual parameter Modifying the formal parameter inside the function directly affects the actual parameter
Formal Parameter Type Ordinary variable type (e.g., int) Reference type (e.g., int &)
Applicable Scenarios When only reading the actual parameter’s value is needed (no modification) When modifying the actual parameter is required, or large objects are passed to avoid copy overhead
Notes Can accept constants, expressions, etc. Must bind to an existing variable (cannot be a temporary value)

四、Tip: How to Choose?

  • Use Pass-by-Value: When you only need to “read” the actual parameter’s value and do not need to modify it (e.g., calculating the square of a number).
  • Use Pass-by-Reference: When you need to “modify” the actual parameter’s value, or when passing large objects (e.g., arrays, large structs) to avoid copy overhead.
  • Avoid Confusion: No need to add & when calling a reference parameter; just pass the variable name directly (e.g., swap_reference(x, y)).

总结

Pass-by-value is like “copy-pasting”—modifying the copy does not affect the original. Pass-by-reference is like “directly borrowing”—modifying the formal parameter is equivalent to modifying the original variable. Remember these core differences to choose the appropriate parameter passing method based on your needs!

Xiaoye