How to pass arguments by reference in a Python function?


In Python, you cannot pass arguments by reference like you can in other programming languages like C++. However, there is a way to simulate pass by reference behavior in Python using mutable data types like lists and dictionaries.

In Python, arguments are passed by assignment, which means that the parameter name in the function definition becomes a reference to the object passed as an argument. Therefore, there is no direct way to pass arguments by reference in Python.

However, you can achieve a similar effect by passing a mutable object like a list or a dictionary to a function and then modifying it inside the function.

Here are some code examples that demonstrate this

Example

In this example, we define a function add_to_list that takes two arguments, lst and num. We pass my_list to the function as lst and the integer 4 as num. Inside the function, we append the value of num to the end of lst, and since lists are mutable data types, this modifies the original list. Therefore, when we print my_list outside of the function, we can see that it has been modified to [1, 2, 3, 4].

def add_to_list(lst, num):
    lst.append(num)
my_list = [1, 2, 3]
add_to_list(my_list, 4)
print(my_list)

Output

[1, 2, 3, 4]

Example

In this example, we define a function modify_dict that takes three arguments, dct, key, and value. We pass my_dict to the function as dct, the string 'b' as key, and the integer 4 as value. Inside the function, we modify the value of the key key in the dictionary dct to be value. Since dictionaries are also mutable data types, this modifies the original dictionary. Therefore, when we print my_dict outside of the function, we can see that it has been modified to {'a': 1, 'b': 4, 'c': 3}.

def modify_dict(dct, key, value):
    dct[key] = value
my_dict = {'a': 1, 'b': 2, 'c': 3}
modify_dict(my_dict, 'b', 4)
print(my_dict) 

Output

{'a': 1, 'b': 4, 'c': 3}

Example

In this example, we define a function called increment_list() that takes a list as an argument and increments each element of the list by 1 using a for loop. We then create a list called numbers with the values [1, 2, 3], call the increment_list() function with numbers as the argument, and print the modified numbers list.

def increment_list(lst):
    for i in range(len(lst)):
        lst[i] += 1

numbers = [1, 2, 3]
increment_list(numbers)
print(numbers)  

Output

[2, 3, 4]

Example

In this example, we define a function called add_to_dict() that takes a dictionary, a key, and a value as arguments and adds the key-value pair to the dictionary. We then create a dictionary called scores with the values {"Alice": 80, "Bob": 90}, call the add_to_dict() function with scores, "Charlie", and 95 as arguments, and print the modified scores dictionary.

def add_to_dict(dictionary, key, value):
    dictionary[key] = value

scores = {"Alice": 80, "Bob": 90}
add_to_dict(scores, "Charlie", 95)
print(scores)  # Output: {"Alice": 80, "Bob": 90, "Charlie": 95}

Output

{'Alice': 80, 'Bob': 90, 'Charlie': 95}

Example

In this example, we define a function called replace_string() that takes a string and a new string as arguments and assigns the new string to the variable holding the original string. We then create a string called message with the value "Hello, world!", call the replace_string() function with message and "Goodbye, world!" as arguments, and print the original message string. Notice that the message variable is not modified by the function because strings are immutable in Python.

def replace_string(string, new_string):
    string = new_string
message = "Hello, world!"
replace_string(message, "Goodbye, world!")
print(message)  # Output: "Hello, world!"

Output

Hello, world!

Updated on: 10-Aug-2023

460 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements