Estimated reading time: 3 minutes
To start off passing by reference basically means that you are telling the program you have written to pass a variable to a function.
When passing arguments to a function, it is important to understand the meanings first.
As a result, when you pass by reference to a function, the variable data gets processed and changes, but the original value does not change.
On the other hand, when passing by value, you are passing a copy of the data into a piece of your program, and it will not get changed.
It should be noted, as will be shown below, values can change on the original variable, it depends if they are mutable.
Example passing by reference and value – no changes to the original variable
def addition(var):
var+=1
print("Argument inside the function:",var)
var = 1
print("Passed by value before the function call,correct as nothing has been actioned on the var.")
print(var) # The code at this point has passed by value, notice the original value has not changed
print("Passed by reference, notice the value has changed, the original var value is copied in.")
addition(var) # This changes the var as a result of the function processing the var argument passed to it.
print("Notice now we have called the function, but it has not changed the original var value.")
print(var)
And the output is as follows:
Output:
Passed by value before the function call,correct as nothing has been actioned on the var.
1
Passed by reference, notice the value has changed, the original var value is copied in.
Argument inside the function: 2
Notice now we have called the function, but it has not changed the original var value.
1
Example passing by reference and value – changes to the original variable
So in the above example, the original values have not been amended. Now we are going to show you an example similar to the above where the value changes.
def addition(var):
var.append('2')
print("Argument inside the function:",var)
var = ['1'] # Lists are mutable, hence their values can be updated.
print("Passed by value before the function call,correct as nothing has been actioned on the var.")
print(var) # The code at this point has passed by value, notice the original value has not changed
print("As lists are muteable, changing a list in a function updates the original list as well.")
print("")
print("Passed by reference, notice the value has changed by the function, the original var value is copied in, has also changed.")
addition(var) # This changes the var as a result of the function processing the var argument passed to it.
print("")
print("Notice now we have called the function, but it has changed the original var value, as lists are muteable, they will also get changed outside the function.")
print(var)
And the output is:
Output:
Passed by value before the function call,correct as nothing has been actioned on the var.
['1']
As lists are muteable, changing a list in a function updates the original list as well.
Passed by reference, notice the value has changed by the function, the original var value is copied in, has also changed.
Argument inside the function: ['1', '2']
Notice now we have called the function, but it has changed the original var value, as lists are muteable, they will also get changed outside the function.
['1', '2']
So one lesson to be learned is that when passing by reference or value, there needs to be an understanding of the original data type of the variable being passed.
As a result, this may lead to a number of opportunities as follows:
- Using lists, and passing by reference will help to update them when new data comes in that needs to be added.
- On the other hand, if you want to ensure that certain pieces of data are immutable, just use a variable that cannot be changed due to its data type.