Call by Value vs Call by Reference in Python | Explained with Code Example

Call by Value vs Call by Reference in Python | Explained with Code Example

“Call by value” and “call by reference” are the types of argument we pass to the function.

In Python, we pass the object reference to the function instead of the object itself. It is called as a “call by object reference”.

However, how the object behaves inside and outside the function depends on the mutability of the object.

If you find it a little bit confusing, don’t worry. I will explain each point with a coding example.

Keep reading.

Call by Value (Immutable Object)

In Python, when you pass an immutable object to the function it acts as a call by value.

Immutable objects include objects of data types int (integer), float, str (string), tuple, etc.

You can check the difference between mutable and immutable objects in Python.

Let’s take an example where we are passing an integer/numeric value to the function.

def fuct_call_by_value(val):
    val = val + 7
    print(f"Value inside function: {val}")

value = 2
fuct_call_by_value(value)
print(f"Value outside function: {value}")

Output:

Value inside function: 9
Value outside function: 2

If you look into the above example code snippet, you can see the updated value is not retained outside of the function. Variable/Object “value” remains unchanged.  This behavior is similar to what we call “call by value”.

Call by Reference (Mutable Object)

When you pass any mutable object to the function as an argument, you pass the object as a reference.

The mutable object includes objects of data types list, dict (dictionary), etc.

Any modification you perform to the object inside the function will also affect the object outside the function.

Let’s take another example, where we are passing a Python list as an argument.

def func_call_by_reference(sample_list):
    sample_list.append(20)
    print(f"Inside function: {sample_list}")

sample_list = [5, 10, 15]
func_call_by_reference(sample_list)
print(f"Outside function: {sample_list}")

Output:

List inside function: [5, 10, 15, 20]
List outside function: [5, 10, 15, 20]

You can see the value of the Python list sample_list, is the same inside and outside the function.

This behavior is similar to the “call by reference”

Difference between Call by Value and Call by Reference in Python

Let’s observe the behavior of passing mutable and immutable objects in the above examples.

  • The program will modify the actual object inside the function if the object is mutable.
  • If the object is immutable that you pass to the function, it creates a new object instead of modifying the actual object.

A Call-by-Reference behavior to the Immutable Object

What if you want to implement call-by-reference behavior to the immutable object?

For that, you have to assign the value returned from the function to the outside object.

Here is an example where we are modifying the same code we have implemented as part of the “call by value” example. 

def fuct_call_by_value(val):
    val = val + 7
    print("Value inside function:", val)

value = 2
Value = fuct_call_by_value(value)
print(f"Value outside function: {value}")

Output:

Value inside function: 9
Value outside function: 9

Conclusion

Hope you understand the difference between call by value and call by reference in Python. Also, I have explained what is a call-by-object reference in Python with coding examples. If you have any doubts, write in the comment section below. 

Leave a Reply

Your email address will not be published. Required fields are marked *