Comparisons and explanations · Intermediate

Pass by value versus pass by reference

Use C++ integer, reference and pointer examples to distinguish copying an argument value from modifying a shared target.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Specify the language and parameter type
  2. Compare an integer parameter with a reference
  3. A copied pointer can still modify its target
  4. Lifetime and ownership are separate contracts
  5. Make mutation intent visible in the API
  6. Ask Logeia about this articleCheck your understanding

Specify the language and parameter type

Pass-by-value and pass-by-reference compare how parameters relate to caller-provided values and objects. Terminology differs across languages, so the examples here use C++ int, int& and int*. A value parameter is initialized as a separate parameter value; a reference parameter binds to a caller-supplied object. However, a copied value can itself be a pointer or another handle to shared storage. Copying the handle does not necessarily duplicate the target. Avoid the blanket rule that pass-by-value cannot change anything visible to the caller. Ask which value is passed and what objects that value permits the function to access. That distinction remains useful when moving between languages with different object and parameter models.

Compare an integer parameter with a reference

Let x initially equal three. Assigning nine to a inside f(int a) changes the parameter, so x remains three after the call. In g(int& a), if the reference binds to x, assigning nine through a changes x itself. The distinction comes from the parameter type and binding rules, not the spelling of a variable name or the number of statements in the function. A reference also does not imply that modification is always permitted or intended: const int& offers a read-only access path. Separate aliasing from write permission. These simple integer examples make the semantics visible before introducing copying costs, object ownership or language-specific optimizations that can otherwise obscure the basic relationship.

A copied pointer can still modify its target

Calling h(int* p) with &x passes the pointer value by value. Nevertheless, assigning through *p changes the pointed-to x. Assigning nullptr to p instead changes only the local parameter value, not a separate pointer variable held by the caller. Trace assignment to the handle separately from assignment through the handle. If changing the caller’s pointer variable is genuinely required, C++ can express that through a reference, another level of indirection or a returned result. The clearest API may simply return the replacement value. Additional indirection is not automatically a better design; use the interface that makes mutation and ownership requirements evident to a reader of the call site.

C++
#include <cassert>
void by_value(int x) { x = 9; assert(x == 9); }
void by_reference(int& x) { x = 9; }
void through_pointer(int* p) { *p = 11; p = nullptr; }
int main() {
    int x = 3;
    by_value(x); assert(x == 3);
    by_reference(x); assert(x == 9);
    int* p = &x;
    through_pointer(p); assert(x == 11 && p == &x);
}

Lifetime and ownership are separate contracts

Receiving a reference or pointer does not alone specify how long its target remains valid. A function that saves an alias can retain a dangling access path if the object later disappears. Value parameters also do not universally mean deep duplication: types can implement shallow copies, shared ownership, moves or other behavior. C++ copy elision and owning smart pointers introduce further distinctions from the integer examples. When changing a parameter strategy for performance, compare mutability, retention, ownership and cross-thread access as well as copying work. An interface that is cheap to call but permits an invalid retained alias has not improved the application’s correctness or its usable performance contract.

Make mutation intent visible in the API

Distinguish passing a small value, borrowing a large object for reading, modifying an object and transferring ownership. Express that intent through the type and documentation. For a swap operation, decide whether it modifies caller objects or returns a new pair containing the swapped values. Tests can separately inspect parameter reassignment, target mutation and the original value after return. Do not import another language’s phrase about passing objects and assume it means a C++ reference parameter. Follow the actual language and type contract, identifying the variable, the value it holds and any target reachable through that value. This method resolves many apparent contradictions without relying on an ambiguous slogan.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Build your CS foundations · STEP 2

Understand the systems behind the web · STEP 2

Prepare for AI systems · STEP 3

Choose a learning path