Python Programming - Pass by Value vs. Pass by Reference

Python Programming – Pass by Value vs. Pass by Reference

As we know that the two-way communication between function caller and function definition is achieved through arguments, which we pass by the function call. In Python, language arguments can be passed by value and by object reference.

Pass by Value

This concept can be understood by considering the program for passing arguments by value in Code: 6.12. It is perceived from the output of the program that a list object is created with values 1, 2, 3 and it is passed as an argument to the function update( ). In the function definition of update, we see that the list values are modified to 100, 200, 300 and the result is printed, which displays the new values. However, when the function update() returns back and the value of the list is printed out to be the old ones, i.e., 1, 2, 3, and not the updated ones. This concept is known as a pass-by value, which signifies that if any modifications are made to the values in the function definition then it does not make any effect on the arguments of the caller function.

Code: 6.12, Illustration of call by value.

# This program demonstrates the concept of call by value

#function definition
def update(list):

‘This function updates the passed values’
list=[ 100, 200, 300]
print(‘inside function’, list)
return

# Function call of update() function
list=[l, 2, 3]
update(list)
print(‘after returning from function’, list)

Output

inside function [100, 200,-300]
after returning from function [1, 2, 3]

Pass by Object Reference

A program representing the concept of pass-by object reference is given in Code: 6.13. We can see from this program that unlike the call by value, where the list values were modified in the function definition of Code: 6.12, here in Code 6.13., the list function append is used to add more items to the list. From the output, it is observed that the new values are appended after the old ones and a similar result is printed both in the function definition and after the function call.

Code: 6.13. Illustration of call by reference.

# This program demonstrates the concept of call by reference

#function definition
def update(list):
‘This changes a passed into this function’
list.append([100, 200, 300])
print(‘inside function’, list)
return

# Function call of update() function
list=[l, 2, 3]
update(list)
print(‘after returning from function1, list)

Output

inside function [1, 2, 3, [100, 200, 300]]
after returning from function [1, 2, 3, [100, 200, 300]]

After, observing the results of both call by value and call by reference, it is perceived that in Python language, the arguments are passed by object reference intrinsically. Any change, made to the arguments in the function definition does not affect the object on the function call side. However, if any in-build function such as append() is associated with the function argument in the function definition, then it definitely makes changes to the object and the object value gets updated on both function definition as well as function call side.

Python Tutorial

Leave a Reply

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