Pass by reference or Value

Ridwan Yusuf
3 min readJan 17, 2023

--

In this guide we'll discuss what happens when we pass argument of different data types(e.g. String, Integer, Float, Set, Dictionary(Hash Map)) to a function call.

Sometimes when we have a function, we need to pass some arguments into the function call. Actual data passed to a function while calling it are called arguments while parameters refers to the listed variables(i.e. its dependencies) during the function declaration.

[*]Function declaration

def add_number(a: int, b: int)->int:
return a+b

In the function declaration up here, a and b are the two parameters function ‘add_number’ needs

[*]Function call/execution

add_number(2,3)

We invoked/called function add_number. 2 and 3 are therefore called arguments passed to the function 'add_number'
[*]Output
5

So what happens under the hood whenever we declare a variable and assign value?
x = 10
Two things here:
- the computer allocates a memory address to be able to identify x
- the computer holds its value on the other hand.

- When immutable data types(e.g. int, string, boolean,etc.) are passed into a function call, only their values are copied into the function. That
means any changes to the variables inside the function won't affect them their initial values.

Meanwhile if the data passed to a function call are mutable(e.g. dictionary, list, set, etc), changes to those variables inside the function affects their original values because their actual reference(i.e.object location in the memory address) are passed

Enough of explanation. A simple example puts more clarity

[*]

def do_something_on_list(list):
list.append("Four")
print("List inside function", my_list)

my_list = ["One", "Two", "Three"]
do_something_on_list(my_list)
print("My list after function call", my_list)

[*]Output
List inside function ['One', 'Two', 'Three', 'Four']
My list after function call ['One', 'Two', 'Three', 'Four']

We declared a mutable data type, my_list, which was latter passed to function "do_something" function. my_list was updated in the function
but after function call(i.e. do_something(my_list)), we noticed that the initial variable (my_list) as been updated outside of the function.

This is so because reference to "my_list" was passed into the function. So any changes to the variable inside the function affect the original variable(i.e. my_list). In fact, any operation on the variable inside the function is an actual operation on the original variable because reference is passed.

Let’s take a look for immutable data types like int

def do_something_on_string(a,b):
a = 22
b = 33
print("A inside the function", a)
print("B inside the function", b)

a = 2
b = 3
do_something_on_string(a,b)


print("A after function call",a)
print("B after function call",b)

[*]Output
A inside the function 22
A inside the function 33
A after function call 2
B after function call 3

Since an immutable data type(i.e. int) are passed into the function call, their values are copied into the function. That means any changes to the variable inside the function does not affect their initial value

In conclusion, it all depends on what you intend by your program. For instance, if you don't want mutable like(dictionary) to be passed as reference because you want to preserve the initial data, a technique like deep copy would work. But if what you want for immutable is to be modify the initial data, then using global statement as explained in Scope post would work.

Though modifying global variables is generally discouraged because it can lead to code that is difficult debug or understand.

Thanks for reading.

--

--

Ridwan Yusuf
Ridwan Yusuf

Written by Ridwan Yusuf

RidwanRay.com -I provide engineers with actionable tips and guides for immediate use

No responses yet