REW

How To Pass Parameters To Function Using Call By Reference?

Published Aug 29, 2025 6 min read
On this page

In call by reference, you pass a function the memory address of a variable, rather than a copy of its value. This allows the function to directly access and modify the original variable in the calling function's memory space, and any changes are reflected outside the function's scope. Call by reference is implemented differently across programming languages, with some offering native support and others requiring the use of pointers to achieve the same effect.

*Note: It's important to distinguish between "call by reference" and "call by value" because they handle data in fundamentally different ways.

  • Call by value: The function receives a copy of the argument, and changes do not affect the original variable.
  • Call by reference: The function receives the argument's memory address, and changes directly affect the original variable.*

How to pass parameters by reference in C++

C++ offers a powerful and direct way to pass parameters by reference using the & symbol.

Syntax

return_type function_name(type& parameter_name) {
    // Operations on parameter_name will modify the original argument
}

Use code with caution.

Example: Swapping two numbers

This example demonstrates how & is used in the function signature to enable changes to the original variables a and b.

#include <iostream>
using namespace std;
// This function receives parameters by reference.
void swap_numbers(int &x, int &y) {
    int temp = x;
    x = y;
    y = temp;
}
int main() {
    int a = 10;
    int b = 20;
    cout << "Before swap: a = " << a << ", b = " << b << endl;
    // The function call does not require any special syntax.
    swap_numbers(a, b);
    cout << "After swap: a = " << a << ", b = " << b << endl;
    return 0;
}

Use code with caution.

Output:

Before swap: a = 10, b = 20
After swap: a = 20, b = 10

This behavior is not possible with call by value, where the main() function's variables would remain unchanged.

How to simulate call by reference in C

The C programming language does not have native support for call by reference. Instead, it uses pointers to achieve the same functionality. A pointer is a variable that stores the memory address of another variable.

Syntax

return_type function_name(type* parameter_name) {
    // Operations on *parameter_name will modify the original argument
}

Use code with caution.

Example: Swapping two numbers with pointers

Here, the function receives pointers to the memory addresses of x and y. It uses the dereference operator * to access and modify the values at those addresses.

#include <stdio.h>
// This function receives pointers as parameters.
void swap_numbers(int *x, int *y) {
    int temp = *x;
    *x = *y;
    *y = temp;
}
int main() {
    int a = 10;
    int b = 20;
    printf("Before swap: a = %d, b = %d\n", a, b);
    // The address-of operator '&' is used to pass the addresses of a and b.
    swap_numbers(&a, &b);
    printf("After swap: a = %d, b = %d\n", a, b);
    return 0;
}

Use code with caution.

Output:

Before swap: a = 10, b = 20
After swap: a = 20, b = 10

How argument passing works in Python

Python uses a model commonly referred to as "call by object reference" or "pass by assignment". This differs from a true call-by-reference model. Here’s how it works with mutable and immutable objects:

  • Immutable objects (e.g., integers, strings, tuples): When you pass an immutable object to a function, you are passing a reference to that object. However, because immutable objects cannot be changed in-place, reassigning the parameter inside the function creates a new object and doesn't affect the original.
  • Mutable objects (e.g., lists, dictionaries): When you pass a mutable object, the function receives a reference to the same object in memory. Any changes made to the object in-place will affect the original variable.

Example: Mutable vs. Immutable objects in Python

# Function for an immutable object
def modify_immutable(num):
    print("Inside function (before change):", num)
    num = 100  # Creates a new integer object
    print("Inside function (after change):", num)
# Function for a mutable object
def modify_mutable(my_list):
    print("Inside function (before change):", my_list)
    my_list.append(4)  # Modifies the original list in-place
    print("Inside function (after change):", my_list)
# Immutable example
number = 10
modify_immutable(number)
print("Outside function:", number)
# Output:
# Inside function (before change): 10
# Inside function (after change): 100
# Outside function: 10
print("----------")
# Mutable example
my_list = [1, 2, 3]
modify_mutable(my_list)
print("Outside function:", my_list)
# Output:
# Inside function (before change): [1, 2, 3]
# Inside function (after change): [1, 2, 3, 4]
# Outside function: [1, 2, 3, 4]

Use code with caution.

How argument passing works in Java

Java is strictly "call by value". When you pass an object to a method, you are passing a copy of the object's reference, not the object itself.

  • Passing primitive types: The value is copied, and the original is unchanged, just like a traditional call-by-value system.
  • Passing object references: The reference to the object is copied. The method can use this copied reference to access and modify the original object's state. However, if the method reassigns the copied reference to a new object, the original reference remains unchanged.

Example: Modifying an object in Java

This example shows how changes to an object's fields are visible outside the method, but reassigning the reference variable is not.

class MyObject {
    int value;
    MyObject(int v) { this.value = v; }
}
public class Main {
    public static void modify_object(MyObject obj) {
        System.out.println("Inside method (before change): " + obj.value);
        obj.value = 50; // Modifies the object's field
        System.out.println("Inside method (after change): " + obj.value);
    }
    public static void reassign_object(MyObject obj) {
        obj = new MyObject(100); // Reassigns the local reference
        System.out.println("Inside method (after reassign): " + obj.value);
    }
    public static void main(String[] args) {
        MyObject my_obj = new MyObject(20);
        // Example 1: Modifying the object's state
        modify_object(my_obj);
        System.out.println("Outside method (after modify): " + my_obj.value);
        System.out.println("----------");
        // Example 2: Reassigning the reference
        reassign_object(my_obj);
        System.out.println("Outside method (after reassign): " + my_obj.value);
    }
}

Use code with caution.

Output:

Inside method (before change): 20
Inside method (after change): 50
Outside method (after modify): 50
----------
Inside method (after reassign): 100
Outside method (after reassign): 50

Use cases and considerations for call by reference

Advantages

  • Efficiency: For large data structures like arrays or complex objects, passing by reference avoids the overhead of creating a costly copy.
  • Multi-value return: It allows a function to effectively "return" multiple values by modifying the original variables passed into it.
  • In-place modification: Some algorithms and functions are designed to modify data in place, which is only possible with call by reference.

Disadvantages

  • Side effects: Since the function can modify the original data, it can lead to unexpected "side effects" that are difficult to debug. The function's behavior is less transparent than with call by value.
  • Safety: Without proper care, allowing a function to modify variables outside its scope can introduce bugs and make the program less safe. Using const references in C++ can mitigate this risk by preventing modifications.

When to use call by reference

  • To modify a variable in the calling function. This is the primary use case, such as the swap function example.
  • With large data structures. When working with large objects, arrays, or structs, passing by reference is more memory-efficient and faster than creating a full copy.
  • For "out" parameters. In some cases, a function may need to return a value but also populate other variables. By passing those variables by reference, the function can modify them directly.
Enjoyed this article? Share it with a friend.