Skip to content
IT glossary
ProgrammingUpdated December 3, 2026

Pass by value vs by reference

Передача по значению и по ссылке

A function can receive its own copy of an argument’s value or use a parameter as another name for the caller’s variable. Only in the second case does assigning to the parameter change that variable.

What the function receives

When you call update(balance), balance is the argument and the name in update’s declaration is the parameter. The distinction between pass by value and pass by reference is about what that parameter stands for, not whether the function can change anything outside itself.

  • Pass by value: The parameter has its own copy of the argument’s value. Assigning to the parameter does not assign to the caller’s variable.
  • Pass by reference: The parameter is an alias for the caller’s variable. Assigning through the parameter writes to that variable.

These are rules about what a program can observe. They do not tell you whether the compiler physically copies bytes or puts a parameter on the stack.

A C# example

static void SetLocal(int n) { n = 42; }
static void SetOriginal(ref int n) { n = 42; }

int score = 10;
SetLocal(score);        // score is still 10
SetOriginal(ref score); // score is now 42

SetLocal replaces its own parameter value. SetOriginal writes to the caller’s score. C# puts ref at both the declaration and the call site, so you can see from the call that the method can overwrite the variable.

Copying a reference is not passing by reference

The word “reference” can describe either a kind of value or a way of passing an argument. Java, for example, passes all arguments by value. When you pass a list, the method gets a copy of the reference to that list. There is still only one list: items.add("x") changes it, and the caller sees the change. But items = new ArrayList<>() changes only which list the local parameter refers to. It does not replace the caller’s reference.

The same distinction applies to a C pointer passed by value: writing through the pointer can affect shared data, but assigning a new address to the parameter does not update the caller’s pointer variable.

Why it matters in practice

Use the function’s signature and contract to check what it can change. In C++, a T& parameter lets a function update an existing object, as an in-place sort might. A const T& can avoid copying a large object while preventing ordinary writes through that parameter; it does not make the object immutable everywhere else. When the function simply computes a replacement value, returning it can make the update easier to spot: score = calculateScore(score).

How it comes up in interviews

A common question asks whether swap(a, b) can exchange two variables in the caller. If the parameters are passed by value, swapping them only swaps local values, even when those values are object references. If the parameters alias a and b, the swap can update both caller variables. For code-reading questions, read each line as either an assignment to a variable or a mutation of an object reached through that variable. Then ask who can observe the change.