coding
variable
reference

How do I pass a variable by reference?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In most programming languages, passing a variable by reference means that a function receives a reference to the original variable rather than a copy. This allows the function to modify the original variable directly.

Here’s how this is handled in different languages, including Python, C++, Java, and JavaScript:


1. Python

In Python, all variables are references to objects, but the behavior depends on whether the object is mutable or immutable.

Mutable Objects (Lists, Dictionaries, etc.)

Mutable objects can be modified within a function.

python
1def modify_list(my_list):
2    my_list.append(4)  # Modify the original list
3
4numbers = [1, 2, 3]
5modify_list(numbers)
6print(numbers)  # Output: [1, 2, 3, 4]
  • The function modifies the original numbers list because lists are mutable.

Immutable Objects (Integers, Strings, etc.)

For immutable objects, reassignment does not affect the original variable.

python
1def modify_num(x):
2    x += 10  # Creates a new object; does not modify the original variable
3
4num = 5
5modify_num(num)
6print(num)  # Output: 5

Simulating Pass-by-Reference with Containers

To pass an immutable value "by reference," wrap it in a mutable container like a list or dictionary:

python
1def modify_value(container):
2    container[0] += 10  # Modify the value inside the container
3
4num = [5]  # Use a list to hold the value
5modify_value(num)
6print(num[0])  # Output: 15

2. C++

In C++, you can explicitly pass variables by reference using the & operator in the function signature.

Example:

cpp
1#include <iostream>
2using namespace std;
3
4void modify(int &x) {  // Pass by reference
5    x += 10;
6}
7
8int main() {
9    int num = 5;
10    modify(num);
11    cout << num;  // Output: 15
12    return 0;
13}

Here:

  • int &x means x is a reference to the original variable.

3. Java

Java uses pass-by-value for primitive types and pass-by-reference-value for objects. You cannot directly pass a variable by reference, but you can modify object properties.

Example:

java
1class Wrapper {
2    int value;
3}
4
5public class Main {
6    public static void modify(Wrapper obj) {
7        obj.value += 10;  // Modify the object's property
8    }
9
10    public static void main(String[] args) {
11        Wrapper wrapper = new Wrapper();
12        wrapper.value = 5;
13
14        modify(wrapper);
15        System.out.println(wrapper.value);  // Output: 15
16    }
17}
  • Objects are passed by "reference value," so you can modify their internal state.

4. JavaScript

JavaScript is pass-by-value for primitives and pass-by-reference-like for objects.

For Objects:

You can modify the properties of an object passed to a function.

javascript
1function modify(obj) {
2    obj.value += 10;  // Modify the property
3}
4
5const data = { value: 5 };
6modify(data);
7console.log(data.value);  // Output: 15

For Primitives:

Primitives (e.g., numbers, strings) are passed by value, so they cannot be modified directly.

javascript
1function modify(x) {
2    x += 10;  // Does not affect the original variable
3}
4
5let num = 5;
6modify(num);
7console.log(num);  // Output: 5

Summary Table

LanguagePass-by-Reference SupportExample
PythonNo explicit pass-by-referenceUse mutable objects or containers like lists.
C++Yes, with & operatorvoid modify(int &x) &#123; x += 10; &#125;
JavaNo explicit pass-by-referenceUse objects to modify internal properties.
JavaScriptObjects are reference-likeModify object properties, but primitives stay unmodified.

Key Takeaway

  • In Python and JavaScript, use mutable objects (lists, dictionaries, or objects) to simulate pass-by-reference behavior.
  • In C++, use the & operator for true pass-by-reference.
  • In Java, pass an object and modify its properties since primitive types are pass-by-value.

Course illustration
Course illustration

All Rights Reserved.