How do I pass a variable by reference? Do you know? If not, don’t worry, because I am here to make you understand the same. We will learn how to pass a variable by reference. Just to start with, there are basically two ways in which we can pass arguments or parameters(as we also know them) to a function. These two methods are Pass-by-Value and Pass-by-Reference. We will now see what these are, and then we will take a closer look at passing variables by Pass-by-Reference.

How do I pass a variable by reference?

In this section, we will see the two methods with the help of some examples. So as to make the topics clearer and comprehensive.

Pass-by-Value

  • A copy of the argument passed to the function is created.
  • And, when an operation takes place on the copy, the actual value does not change.
  • What changes is, only the value of the copy that is created within the function.

Let’s see an example to demonstrate this:

def func(x): 
    x += 25 
    print("Value in function call ~~> ", x)
x = 30 
print("Value before the function call ~~> ", x)
func(x)
print("Value after the function call ~~> ",x)

The output of the execution of the above code is :

Value before the function call ~~>  30
Value in function call ~~>  55
Value after the function call ~~>  30
  • It is so because, as you come to know above, during pass-by-value, a copy of the argument generates.
  • The changes are then made to that copy; same as here and hence, there is no effect of the same on the original value.
  • Therefore, the original value is output after the function call.

Pass-by-Reference

  • In this case, we pass reference to the original argument; and when an operation is performed on the argument, the actual value also changes.
  • It changes the value both in the function and also in the global variable.

Let’s take an example to demonstrate this:

def func(x): 
    x.append('ello') 
    print("Value in function call ~~> ",x)
x = ['H']
print("Value before the function call ~~> ", x)
func(x)
print("Value after the function call ~~> ",x)

The output of the above program is as below:

Value before the function call ~~>  ['H']
Value in function call ~~>  ['H', 'ello']
Value after the function call ~~>  ['H', 'ello']
  • Since a list is used here, its reference is passed to the function.
  • When we make changes to the list, its effect we can see after the function call.

Python Methodology

Python basically uses the method of Call-by-Object Reference, also termed as Call-by-Assignment. This means that it depends on the type of argument passed.

  • If you pass immutable objects, then the method of Call-by-Value comes in use.
  • While when you pass mutable objects, the method of Call-by-Reference comes in use.

Now, what are immutable objects?

These are basically those objects that cannot change their state or value. These are:

– Strings

– Integers

– Tuples

def func(st,n,d):
    st = "Hi"
    n = 10
    d = (23, 62, 81)
    print("Value in function call ~~> ", st, n, d)
st = "Shubham"
n = 1
d = (32, 22, 45)
print("Value before the function call ~~> ", st, n, d)
func(st, n, d)
print("Value after the function call ~~> ", st, n, d)

The output of the above code is:

Value before the function call ~~>  Shubham 1 (32, 22, 45)
Value in function call ~~>  Hi 10 (23, 62, 81)
Value after the function call ~~>  Shubham 1 (32, 22, 45)
  • The output comes as above, because, though the values of st, n and d change inside the function; the original value does not change.
  • It is so because they are passed as value.
  • For this very reason, the same value is output after the function call.

Now, let us talk about mutable objects

These are those objects that can change their state or value. The different types of mutable objects are:

– Lists

Dictionaries

– Sets

How to make a flat list out of a list of lists? Want to know then click here !!

def func(st,n,d):
    st.append(7)
    n["name"]="Shubham"
    d = set({1,2})
    print("Value in function call ~~> ", st, n, d)
st = [11, 21, 31]
n = {"name" : "Rocky"}
d = {15, 25, 35}
print("Value before the function call ~~> ", st, n, d)
func(st, n, d)
print("Value after the function call ~~> ", st, n, d)

It’s output is:

Value before the function call ~~>  [11, 21, 31] {'name': 'Rocky'} {25, 35, 15}
Value in function call ~~>  [11, 21, 31, 7] {'name': 'Shubham'} {1, 2}
Value after the function call ~~>  [11, 21, 31, 7] {'name': 'Shubham'} {25, 35, 15}
  • In this prgram, we pass the various mutable objects (lists, dictionaries and sets, all included) to the function; and their values are changed in it.
  • What we get after the function call is thus the updated value.

How to pass immutable objects in lists and dictionary??

  • To pass immutable objects inside mutable objects (lists/dictionary/sets) we make them the parameters to the function.
  • So, that these can update inside the function.

Let us understand this with the help of an example:

def dict_change(a):
    a["Hi !"] = "The 1st"
def list_change(ls):
    ls[0] = ls[0] + 7
a = {"Hi," : "Shubham"}
ls = [7]
print("Value before it is passed to function is ~~> ")
print("The dictionary ~~> ", a)
print("The list ~~>",ls)

dict_change(a)
list_change(ls)

print("Value after it is passed to function is ~~> ")
print("The dictionary ~~> ", a)
print("The list ~~>",ls)

Output:

Value before it is passed to function is ~~> 
The dictionary ~~>  {'Hi,': 'Shubham'}
The list ~~> [7]
Value after it is passed to function is ~~> 
The dictionary ~~>  {'Hi,': 'Shubham', 'Hi !': 'The 1st'}
The list ~~> [14]

How do you split a list into evenly sized chunks? Click here to learn !!

THE GIST OF IT ALL !! ~~>

By now you are aware of the two methods of passing values as arguments / parameters to a function. Specifically, the Pass-by-Value method and the Pass-by-Reference method. You have also understood a lot more in-depth with the help of numerous examples. Thus, hoping in anticipation that you enjoyed reading this article. And, also take back something worthwhile. With those words, until next time, see ya!! Goodbye !! 🙂 ~~>

**::::::** ::))::((~~>>