Passing Arguments by Reference (C++)

If you use a reference type as a formal argument, you can make a pass-by-reference call to a function. In a pass-by-reference call, the values of arguments in the calling function can be modified in the called function. In pass-by-value calls, only copies of the arguments are passed to the function.

Note: The term pass by reference describes a general method of passing arguments from a calling routine to a called routine. The term reference in the context of C++ refers to a specific way of declaring objects and functions.

Ellipsis arguments cannot be passed as references.

In addition, when the actual argument cannot be referenced directly by the formal argument, the compiler creates a temporary variable that is referenced by the formal argument and initialized using the value of the actual argument. In this case, the formal argument must be a const reference.

Reference arguments declared const can be used to pass large objects efficiently to functions without making a temporary copy of the object that is passed to the function. Because the reference is declared const, the actual arguments cannot be changed by the function. For example:

void printbig (const bigvar&); // Function prototype

When a function printbig is called, it cannot modify the object of type bigvar because the object was passed by constant reference.