I. What Is a Parameter Passing Mechanism
Parameter passing mechanisms are the rules by which a programming language associates the arguments provided by the caller with the parameters declared by the called function during a function call.
For example:
def add(a, b):
return a + b
result = add(10, 20)
Here:
a,b: parameters, the variables declared when the function is defined.10,20: arguments, the values passed when the function is called.- Parameter passing: the process of establishing a correspondence between arguments and parameters.
Parameter passing concerns not only how data is passed in but also determines the following behaviors:
- Whether the function receives a copy of the original data.
- Whether the function can modify data held by the caller.
- Whether reassigning a variable inside the function affects the caller.
- How objects, pointers, and references behave during the call.
- Whether parameter passing involves copying, address passing, or an alias relationship.
Note in particular: the parameter passing mechanism and the data type of the parameter itself are two different dimensions. The same data type may use different parameter passing semantics in different languages.
II. The Basic Process of Parameter Passing
During a function call, the runtime must perform operations such as evaluating arguments, binding parameters, executing the function, and returning.
flowchart TD
A["Call function"] --> B["Evaluate argument expressions"]
B --> C["Associate parameters with arguments"]
C --> D["Create or bind function execution environment"]
D --> E["Execute function body"]
E --> F{"Whether to return"}
F -->|"Yes"| G["Return result"]
F -->|"Exception or early exit"| H["End current call"]
G --> I["Caller continues execution"]
H --> I
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:#E0F0FE,stroke:#93BCE8,color:#1E4976
style E fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style F fill:#FFF5DE,stroke:#E8D29A,color:#334155
style G fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style H fill:#E8EDF2,stroke:#B8C3CE,color:#334155
style I fill:#E5F4EC,stroke:#A3CBB5,color:#334155
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
The implementation details may differ across languages:
- Compiled languages may pass parameters through registers, the stack, or memory addresses.
- Interpreted languages may complete parameter binding through runtime object references and call frames.
- Compilers may perform register allocation, copy elimination, and inlining optimizations on parameters.
These are low-level implementation approaches and cannot be directly equated with parameter passing semantics at the language level.
III. Pass by Value
Pass by value means that the function receives the value of an argument and binds it to a separate storage location for the parameter.
The parameter and the argument are logically independent of each other. When the function modifies the parameter, it does not directly modify the caller's variable.
3.1 Basic Example
Take C as an example:
#include <stdio.h>
void change(int x) {
x = 100;
}
int main() {
int a = 10;
change(a);
printf("%d\n", a); // 10
return 0;
}
Execution process:
flowchart TD
A["Call change(a)"] --> B["Read the value of a: 10"]
B --> C["Copy the value to parameter x"]
C --> D["Modify x = 100 inside the function"]
D --> E["Parameter x becomes 100"]
E --> F["Function returns"]
F --> G["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:#E5F4EC,stroke:#A3CBB5,color:#334155
style F fill:#E8EDF2,stroke:#B8C3CE,color:#334155
style G fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
3.2 Memory Relationship
This can be understood logically as two independent variables:
| Variable | Initial Value | After Function Execution |
|---|---|---|
Caller a |
10 | 10 |
Parameter x |
10 | 100 |
Modifying x does not affect a.
The independence here is at the level of language semantics; it does not mean the compiler must allocate distinct physical memory locations for the two variables. Optimized machine code may eliminate the actual copy operation, as long as the program's observable behavior remains consistent.
3.3 Characteristics of Pass-by-Value
- The parameter has its own independent variable binding.
- Modifying the parameter itself does not modify the caller's variable.
- It usually requires copying the argument's value, or achieving value semantics in an equivalent way.
- It is suitable for primitive numeric values, booleans, and other arguments that require independent value semantics.
However, pass-by-value does not necessarily mean copying the entire object. For object references or pointer types, only the reference value or address value may be copied.
IV. Pass by Reference
Pass by reference means that the parameter establishes a reference relationship with the caller's variable, and the variable the function accesses through the parameter is exactly the caller's corresponding variable.
Therefore, when the function modifies the variable through the parameter, it directly affects the caller.
4.1 C++ Reference Parameters
#include <iostream>
using namespace std;
void change(int& x) {
x = 100;
}
int main() {
int a = 10;
change(a);
cout << a << endl; // 100
return 0;
}
Here:
void change(int& x)
int& denotes a reference type, and x is another name through which a can be accessed.
Execution flow:
flowchart TD
A["Call change(a)"] --> B["Parameter x binds to variable a"]
B --> C["Modify the variable through x"]
C --> D["The value of a becomes 100"]
D --> E["Function returns"]
E --> F["Caller observes a = 100"]
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:#E8EDF2,stroke:#B8C3CE,color:#334155
style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
4.2 Key Characteristics of References
In C++:
- A reference must be bound to a valid object when it is initialized.
- Once initialized, a reference cannot be rebound to another object.
- Modifying an object through a reference actually modifies the referenced object.
- A reference itself is not an independent object variable; in language semantics, it acts as an alias for the original object.
For example:
int a = 10;
int b = 20;
int& ref = a;
ref = b;
After execution:
a == 20;
b == 20;
Here, ref = b does not rebind ref to b; instead, it assigns the value of b to a, which ref references.
4.3 Const References
C++ also supports const references:
void print(const std::string& value) {
std::cout << value << std::endl;
}
const std::string& has two important characteristics:
- It avoids copying large objects.
- It does not allow the object to be modified through that reference.
It is often used for read-only parameters, balancing performance and interface safety.
Note that const restricts modifications made through the current reference; it does not mean the object is absolutely immutable through other access paths.
V. Pointer Passing
Pointer passing uses the memory address stored in a pointer variable to let a function access data at another location.
One important concept must be distinguished:
In C, pointer parameters themselves are still passed by value.
What is passed is the pointer value, that is, a copy of the address, not pass-by-reference in the C++ sense.
5.1 Modifying a variable through a pointer
#include <stdio.h>
void change(int *p) {
*p = 100;
}
int main() {
int a = 10;
change(&a);
printf("%d\n", a); // 100
return 0;
}
Here:
&a: obtains the address of variablea.p: the pointer parameter that receives the address value.*p: accesses the corresponding object through the address.
Although the pointer parameter is passed by value, the pointer copy and the pointer passed in by the caller have the same target address, so the original variable can be modified through dereferencing.
5.2 Modifying the pointer itself
#include <stdio.h>
void changePointer(int *p) {
int temp = 100;
p = &temp;
}
int main() {
int a = 10;
int *ptr = &a;
changePointer(ptr);
printf("%d\n", *ptr); // 10
return 0;
}
Inside the function, what is modified is the pointer parameter p, not the caller's ptr.
If you need to modify the caller's pointer variable, you need to pass the address of the pointer, that is, a pointer to a pointer:
#include <stdio.h>
#include <stdlib.h>
void allocate(int **p) {
*p = malloc(sizeof(int));
if (*p != NULL) {
**p = 100;
}
}
int main() {
int *ptr = NULL;
allocate(&ptr);
if (ptr != NULL) {
printf("%d\n", *ptr); // 100
free(ptr);
}
return 0;
}
Here:
pis a pointer to a pointer.*pis the caller's pointer variable.**pis the integer object pointed to by the caller's pointer.
The difference between pointer passing and reference passing cannot be judged only by whether the original data can be modified; you also need to determine whether what is modified is the data itself or the variable that stores the address.
6. Pass by Sharing
Pass by sharing, also called call by sharing, is a parameter behavior often encountered in languages such as Python and Java.
Its core is:
The function receives a copy of the object reference value; the caller and the function parameter can point to the same object, but the two variable bindings are independent of each other.
This means:
- The internal state of the shared object can be modified through the parameter.
- Reassigning the parameter does not change the caller variable's binding.
6.1 Python Example
def modify(items):
items.append(100)
data = [1, 2, 3]
modify(data)
print(data) # [1, 2, 3, 100]
Inside the function, the list object is modified through items, so the caller observes the change.
6.2 Rebinding the parameter
def modify(items):
items = [100, 200]
data = [1, 2, 3]
modify(data)
print(data) # [1, 2, 3]
Here items = [100, 200] creates a new list object and binds the local variable items to the new object.
The caller's data still points to the original list.
6.3 Object sharing relationship
flowchart TD
A["Caller variable data"] --> C["List object"]
B["Function parameter items"] --> C
C --> D["Shared object internal state"]
D --> E["append modifies object"]
E --> F["Caller observes change"]
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
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
Here there are two independent variable bindings:
datais bound to the list object.itemsis also bound to the same list object.
Objects are shared; variable bindings are not.
6.4 Parameter Passing in Java
Java also uses pass-by-value.
For primitive types, the primitive value is copied:
public class Main {
static void change(int x) {
x = 100;
}
public static void main(String[] args) {
int a = 10;
change(a);
System.out.println(a); // 10
}
}
For object types, the object reference value is copied:
import java.util.ArrayList;
import java.util.List;
public class Main {
static void modify(List<Integer> list) {
list.add(100);
}
static void reassign(List<Integer> list) {
list = new ArrayList<>();
list.add(200);
}
public static void main(String[] args) {
List<Integer> data = new ArrayList<>();
data.add(1);
modify(data);
System.out.println(data); // [1, 100]
reassign(data);
System.out.println(data); // [1, 100]
}
}