How to Call By Value and Call By Reference in Python

In this Python tutorial, I will show you how to call by value and call by reference in Python.

Both the concepts of call by value and call by reference are very helpful in memory management, data manipulation, and even API development.

In this tutorial, I have explained the meaning of call by value and reference and, with examples, shown how to implement these concepts and how they differ.

Let’s begin,

What does Call By Value and Call By Reference in Python Mean?

In several programming languages, you can pass the data to the function argument in two ways: by value and reference (address).

  • Calling by value means passing the value to the function’s argument; if any changes are made to that value within the function, then the original value outside the function remains the same.
  • Whereas Calling by reference means passing the address of a value to the function’s argument, if any changes are made to that value within the function, they also change the value outside the function.

Understanding these two concepts is very helpful because it allows you to manage your data in a better way.

But I want to tell you one thing: Python does not have the concept ‘call by value’ like languages like C, C++, Java, etc. Instead, it passes the object reference by value.

READ:  How to Create Plots using Pandas crosstab() in Python

This means you pass the immutable objects, such as integers and tuples, to the function argument; it acts like a call by value because the nature of immutable objects prevents any changes within the function from affecting the original objects.

If we talk about the call by reference in Python, mutable objects, such as lists, dictionaries, etc, are passed to the function arguments. Since the function gets the reference to the original object, whatever modification is made to objects changes the original object.

Your concept of call by value and reference must have been clear until now; if not, don’t worry. You will understand it through examples.

Pass By Value and Pass by Reference in Python

Before beginning, remember one thing: the original value doesn’t change in the call by value, but it does change in the call by reference. Also, I will use the words pass or call interchangeably, depending upon the context.

Pass By Reference in Python

As I said, when mutable objects such as lists and dictionaries are passed to the function arguments. Any changes made to these objects within a function by an operation also change the original objects outside the function.

Let’s see, first, create a dictionary named ‘user_profile’ as shown below.

user_profile = {
	'username': 'Tylor',
	'email': '  tylor@icloud.com'
}

In the above code, the dictionary user_profile contains two key-value pairs: username and email as keys and Tylor and tylor@icloud.com as values, respectively.

Now, create a function that updates the user profile email as shown below.

def update_user_profile(user_info, user_email):
	user_info['email'] = user_email.lower().strip()
	print("Updated email inside function:", user_info['email'])

In the above code, the update_user_profile function takes two arguments: the user data through user_info and the user’s new email through user_email that you want to update.

READ:  Matplotlib xlim - Complete Guide

Before calling the function with those values, note the user’s current email; now, let’s call the function with user_profile and new email ‘tylor@gmail.com’ as shown below.

update_user_profile(user_profile,'tylor@gmail.com')
print("Email outside function:", user_profile['email'])
Pass By Reference in Python

Look at the output when you call the function update_user_profile, passing it a dictionary ‘user_profile’ whose email you want to update and the new email ‘tylor@gmail.com’. It updates both emails inside and outside the function.

This means it changes the original dictionary ‘user_profile’ outside the function you created.

This is how to call or pass by reference in Python.

Pass By Value in Python

In pass-by-value, when immutable objects such as integers, strings and tuples are passed to the function arguments. Any changes made to these objects within a function by an operation don’t change the original objects outside the function.

To understand this concept, let’s take a simple example.

Create a variable value and initialize it with 5, as shown below.

value = 5

Create a function named ‘addition’ that accepts the value and increments it by 1, as shown below.

def addition(value):
  value = value + 1
  print('The value inside function',value)

Call the function by passing the variable value (containing 5) you created, as shown below.

addition(value)
print('The value outside function',value)
Pass By Value in Python

Look at the output; the value outside the function is not affected. It contains the same value, which is 5, but the value in the function is incremented by one; it includes the value 6.

Here, when you pass the variable value to function addition(value), the copy of the variable is passed (in reality, it is an immutable object), so within the function, this line value = value + 1 adds the one to the copy of the variable, not on the original one.

READ:  ValueError: setting an array element with a sequence error in Python [4 methods to fix]

So, only the variable value within a function is incremented, not the variable value outside the function.

This is how to pass by value in Python.

Difference Between Pass By Value and Pass By Reference in Python

Let’s understand the pass-by-reference vs. pass-by-value Python.

Call by referenceCall by value
When calling a function in a programming language, the address of the variables is used instead of copying their values; this is known as “Call By Reference.”While calling a function, when we pass values by copying variables, it is known as “Call By Values.”
In this method, a variable itself is passed.A copy of the variable is passed in a call by value.
Change in the variable also affects the variable’s value outside the function.Changes made in a copy of a variable never modify the value of the variable outside the function.
Allows you to change the values of variables by using function calls.It does not allow you to make any changes in the actual variables.
The original value is modified.Original value not modified.
Pass By Reference vs Pass By Value Python

Conclusion

In this Python tutorial, you learned how to call by value and call by reference in Python with examples.

Additionally, You learned what call by value and reference means and the difference between them.

You may like to read: