Back to series
· 7 min read

Pass by Value vs. Pass by Reference


1. Basic Concepts of Pass by Value and Pass by Reference

When a function is called, the caller provides data to the callee through arguments, and the function receives this data through parameters.

Depending on how parameters and arguments are associated, common parameter-passing semantics can be divided into pass by value and pass by reference.

1. Pass by Value

Pass by value means: the function receives a copy of the argument's value and binds it to the parameter.

The parameter and the caller's variable are two independent variables. Modifying the parameter itself inside the function does not directly change the caller's variable.

#include <stdio.h>

void change(int x) {
    x = 100;
}

int main() {
    int a = 10;

    change(a);

    printf("%d\n", a); // 10
    return 0;
}

Execution result:

10

When calling change(a), the function obtains a copy of a's current value.

Modifying x inside the function only affects the parameter x, and does not change the caller's a.

2. Pass by Reference

Pass by reference means: the parameter establishes a direct reference relationship with the caller's variable, and what the function accesses through the parameter is the corresponding variable of the caller.

Modifying the variable through the parameter inside the function directly affects the caller.

C++ example:

#include <iostream>
using namespace std;

void change(int& x) {
    x = 100;
}

int main() {
    int a = 10;

    change(a);

    cout << a << endl; // 100
    return 0;
}

Execution result:

100

Here x is a reference to a. Modifying the value through x is actually modifying a.

3. Core Differences Between the Two

Comparison Dimension Pass by Value Pass by Reference
Parameter relationship Independent variable binding Parameter is associated with the caller's variable
Whether the value is copied Semantically obtains a copy of the value Does not copy the referenced variable's value as an independent parameter
Modifying the parameter Does not directly affect the original variable Affects the original variable
Reassigning the parameter Does not affect the original variable Modifies the referenced variable
Data sharing Depends on the passed value Directly accesses the same variable
Typical language examples C, Java ref parameters in C++ and C#

Note: what is discussed here is the parameter-passing semantics at the language level, not low-level implementation details such as registers, stacks, or memory addresses.

2. How Pass by Value Works

2.1 Variable Copies

Suppose the caller has a variable:

int a = 10;

Call the function:

change(a);

The function receives the parameter:

void change(int x) {
    x = 100;
}

Logically, there are two independent variables during the function's execution:

Variable Initial Value After Modification
a 10 10
x 10 100

The two have the same initial value, but modifying one does not change the other.

2.2 Execution Process

flowchart TD
    A["Caller variable a = 10"] --> B["Call change(a)"]
    B --> C["Read the value of a"]
    C --> D["Create an independent value binding for parameter x"]
    D --> E["Execute x = 100 inside the function"]
    E --> F["x becomes 100"]
    F --> G["Function returns"]
    G --> H["Caller's a remains 10"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style D fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style E fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style F fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style G fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style H fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

2.3 Pass-by-value does not necessarily copy the entire object

This is a very important issue when understanding pass-by-value.

Pass-by-value copies the parameter value, but the parameter value is not necessarily a complete object; it may also be:

  • Basic values such as integers and floating-point numbers.
  • Pointer addresses.
  • Object references.
  • Slice descriptors.
  • Other data structures that contain indirect references.

For example:

void change(int *p) {
    *p = 100;
}

Call:

int a = 10;
change(&a);

Here, a copy of the address value is passed. The function accesses the original variable through the address, so it can modify a.

Therefore:

Pass-by-value does not mean the function can only modify data in the copy. It only means that the formal parameter receives a copy of the argument's value.

If this value itself contains an address or reference pointing to a shared object, the function may still modify the shared object.

III. How pass-by-reference works

3.1 A reference is associated with the original variable

In C++, pass-by-reference can be implemented using reference parameters:

void change(int& x) {
    x = 100;
}

Call:

int a = 10;
change(a);

Inside the function, x is associated with the caller's a.

From the perspective of language semantics, x is another name for a, not an ordinary variable with an independent value.

3.2 Execution process

flowchart TD
    A["Caller variable a = 10"] --> B["Call change(a)"]
    B --> C["Parameter x is bound to variable a"]
    C --> D["Perform assignment through x"]
    D --> E["Original variable a is modified"]
    E --> F["The value of a becomes 100"]
    F --> G["Function returns"]
    G --> H["Caller reads 100"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style D fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style E fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style F fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style G fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style H fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

3.3 Several important characteristics of references

Taking C++ lvalue references as an example:

Characteristic 1: A reference must be initialized

int a = 10;
int& ref = a;

An ordinary lvalue reference cannot be declared without initialization:

int& ref; // compile error

Characteristic 2: A reference cannot be rebound after initialization

int a = 10;
int b = 20;

int& ref = a;

ref = b;

After execution:

a == 20;
b == 20;

ref = b is an assignment operation, not rebinding the reference.

ref still refers to a; it merely assigns the value of b to a.

Feature 3: References can restrict modification permissions

void print(const int& value) {
    // value = 100; // Modification through this reference is not allowed
    std::cout << value << std::endl;
}

const int& is often used for read-only parameter passing of large objects; it avoids unnecessary copying while restricting the function from modifying the object.

4. Differences Between Pointer Passing and Reference Passing

Pointers and references are often discussed together, but they are not the same mechanism.

4.1 Pointer Parameters in C

#include <stdio.h>

void change(int *p) {
    *p = 100;
}

int main() {
    int a = 10;

    change(&a);

    printf("%d\n", a); // 100
    return 0;
}

What happens here is:

  1. The caller obtains the address of variable a.
  2. The address value is passed to the function.
  3. The function accesses the original variable through the pointer parameter p.
  4. After dereferencing, it modifies the original variable.

Key point:

Pointer parameters in C still use value passing; what is copied is the address value stored in the pointer.

4.2 The Pointer Itself Is Also Copied

void changePointer(int *p) {
    int temp = 100;
    p = &temp;
}

Here, what is modified is the pointer variable p inside the function.

The caller's pointer variable will not change as a result.

If you need to modify the caller's pointer variable, you can use a pointer to a pointer:

#include <stdlib.h>

void allocate(int **p) {
    *p = malloc(sizeof(int));

    if (*p != NULL) {
        **p = 100;
    }
}

4.3 Comparison of Pointers and References

Comparison aspect Pointer C++ reference
Essence A variable that stores an address value An alias for an existing object
Can it be null? A null pointer can be used to represent no object A normal reference cannot legally bind to a null object
Can it be redirected? Yes It cannot be rebound after initialization
Accessing the object Requires dereferencing Can be used directly
Supports multiple levels of indirection? Yes Does not directly support pointer-style multiple levels of indirection
Parameter passing The pointer value is passed Reference parameters implement reference passing

It should be noted that C++ references may be implemented under the hood via addresses, but this does not mean that references and pointers are completely identical in language semantics.

5. Object Parameters: Why Value Passing Can Still Modify the Original Object

This is the most easily misunderstood aspect in languages such as Java, Python, and JavaScript.

When a function parameter is of object type, what is copied may not be the object itself, but the object reference value.

The caller and the function parameter may hold different copies of the reference value, but these references point to the same object.

5.1 Java Example

import java.util.ArrayList;
import java.util.List;

public class Main {
    static void modify(List<Integer> list) {
        list.add(100);
    }

    public static void main(String[] args) {
        List<Integer> data = new ArrayList<>();
        data.add(1);

        modify(data);

        System.out.println(data); // [1, 100]
    }
}

Why can modify modify the caller's data?

Because list and data point to the same list object.

Java still passes by value; it is just that this value is an object reference.

5.2 Reassignment Does Not Change the Caller's Reference

static void replace(List<Integer> list) {
    list = new ArrayList<>();
    list.add(200);
}

Call:

List<Integer> data = new ArrayList<>();
data.add(1);

replace(data);

System.out.println(data); // [1]

list is rebound to a new list object, but data still points to the original list.

5.3 Object Reference Relationships

flowchart TD
    A["Caller variable data"] --> C["Original list object"]
    B["Function parameter list"] --> C
    C --> D["Shared object state"]
    D --> E["Modify the object through list"]
    E --> F["data observes the modification"]

    G["Reassign list inside the function"] --> H["New list object"]
    H --> I["Changes only the local variable binding"]
    I --> J["data still points to the original list object"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style C fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style D fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style E fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style G fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style H fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style I fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style J fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

Therefore, it is necessary to distinguish:

  • Modifying the object: accessing the object's internal state through a reference.
  • Modifying the variable: changing the value or binding stored by the variable itself.

The former may affect the caller, while the latter usually only affects the formal parameter inside the function.

VI. Implementations in Different Programming Languages

6.1 C Language

C language function arguments use pass-by-value.

void func(int x);
void func(int *p);

Both are pass-by-value:

  • int x: copies the integer value.
  • int *p: copies the pointer address value.

C does not have the direct reference-parameter syntax that C++ has.

6.2 C++

C++ supports both value parameters and reference parameters.

void func1(int x);        // value parameter
void func2(int& x);       // lvalue reference parameter
void func3(const int& x); // const lvalue reference parameter
void func4(int&& x);      // rvalue reference parameter

It is necessary to distinguish:

  • T: receives the object by value.
  • T&: accesses the object through an lvalue reference.
  • const T&: accesses the object through a read-only lvalue reference.
  • T&&: rvalue reference, commonly used for move semantics and perfect forwarding.