C++ Pass-by-Reference: Why Use the & Symbol for Function Parameters?

### Why Use the & Symbol for Function Parameters? — The Secret of C++ Reference Passing This article explains the necessity of using the & symbol (reference passing) for function parameters in C++. By default, value passing copies a parameter's actual value, preventing the function from modifying the original variable (as seen in the swap function example where value passing fails). A reference is an "alias" for a variable, sharing the same memory with the original variable. When a function parameter is declared with &, it becomes a reference to the original variable, enabling direct modification of external variables. Advantages of reference passing include: directly modifying the original variable, avoiding the waste of copying large objects (e.g., structures, arrays), and resulting in cleaner code compared to pointer passing. It is crucial to distinguish the dual role of &: as the address-of operator (returns a pointer when used as &var) and as a reference declarator (e.g., int &a requires initialization and cannot change its target). Key notes: References must be initialized, cannot be null references, and their target cannot be changed once bound. Applicable scenarios include modifying external variables, handling large objects, and simplifying code. Reference passing uses the & symbol to achieve "direct operation on the original variable," solving the limitations of value passing and serving as a critical feature for efficiently modifying external variables.

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

The article introduces two common parameter passing methods in C++: value passing and reference passing, with the core difference lying in their impact on the original variable. Value passing involves passing a copy of the actual parameter to the function, where the formal parameter is independent of the actual parameter. Modifying the formal parameter does not affect the original variable. For example, when swapping variables, the function modifies the copy, leaving the original variable's value unchanged. This is suitable for scenarios where the original data does not need modification or when the data volume is small. Reference passing transfers a reference (an alias of the variable) to the actual parameter, directly pointing to the original variable's address. Modifying the formal parameter will directly affect the actual parameter. Similarly, when swapping variables, the function modifies the original variable, resulting in the values being swapped. This is suitable for scenarios where the original data needs modification or when passing large objects (such as arrays or structures) to avoid copying overhead. Core differences: Value passing is "copying", while reference passing is "direct borrowing"; the former does not affect the original variable, and the latter does; the former uses ordinary types, and the latter uses reference types (`&`). When choosing, use value passing for read-only or small data, and reference passing for modification needs or large objects. Understanding this difference enables accurate variable manipulation.

Read More