Pass by value is a way of passing a variable to a function. A copy of the variable is created which is then passed through the function.
In this way, the original variable remains unchanged and you can perform all the operations on the copy of the variable that will be passed through the function.
What is Pass By Value in Python?
In Python, pass by value means that when a variable is passed as an argument to a function, a copy of the value of the variable is created and passed to the function.
This copy is distinct from the original variable, and any changes made to the copy inside the function do not affect the original variable outside the function.
For example, consider the following code:
def double(num):
num = num * 2
return num
x = 5
print(double(x))
print(x)
Output
10
5
In this code, the variable x
is passed as an argument to the double()
function.
Inside the function, a copy of the value of x
is created and assigned to the parameter num
. The value of num
is then doubled and returned.
When the print(double(x))
statement is executed, the function is called with x
as its argument, and the result (which is the value of x
multiplied by 2) is printed.
However, when the print(x)
statement is executed, the original value of x
(which is still 5) is printed, because the double()
function did not modify the original variable.
Therefore, in Python, variables can be passed by value, which means that a copy of the value of the variable is passed to the function, and any changes made to the copy inside the function do not affect the original variable outside the function.
What is Pass By Value Example?
Here are some more examples to understand this concept of pass by value in a more efficient and easy way.
Example 1: Passing an Integer Variable by Pass By Value
In this example, you will understand how you can pass an integer variable by using pass by value method.
So. let’s dive into it. Here is the code;
def add_one(num):
num = num + 1
return num
x = 5
print(x)
print(add_one(x))
This function will add one to the number that you will pass through this function.
A copy of the number will be created and one will be added to that copy. The original variable will remain the same.
Output
5
6
In this example, we define a function add_one() that takes an integer argument num.
Inside the function, we create a copy of the value of num and assign it to a local variable called num.
We then add 1 to the local num variable and return it.
When we call the add_one() function with the variable x as an argument, the value of x (5) is passed by value to the function, and a copy of it is created.
The add_one() function modifies the local copy of num but does not affect the original value of x outside the function.
Example 2: Passing a String Variable by Pass By Value
In this example, you will pass a string variable through a function using pass by value.
You will learn to make a function that will capitalize the first letter of a string by using the built-in capitalize() method.
def capitalize_string(s):
s = s.capitalize()
return s
my_string = "hello world"
print(capitalize_string(my_string))
print(my_string)
Output
Hello world
hello world
In this example, we define a function capitalize_string() that takes a string argument s.
Inside the function, we create a copy of the value of s and assign it to a local variable called s.
We then call the capitalize() method on the local s variable to capitalize the first letter of the string and return the modified string.
When we call the capitalize_string() function with the variable my_string as an argument, the value of num (“hello world”) is passed by value to the function, and a copy of it is created.
The capitalize_string() function modifies the local copy of s but does not affect the original value of my_string outside the function.
Using the .upper() method to change the string to uppercase
You can use python’s built-in method .upper() to change your string to uppercase.
Let’s look deep into it.
def uppercase_string(s):
s = s.upper()
return s
my_string = "hello world"
print(uppercase_string(my_string))
print(my_string)
Output
HELLO WORLD
hello world
You can see the string is transformed to uppercase but the original string, my_string is still the same.
That is due to the reason that we passed a copy of the string by pass by value, not the original string.
Example 3: Passing a List through a Function
Now, you will learn how you can pass a list using pass by value in python.
def append_to_list(lst):
lst.append(4)
return lst
my_list = [1, 2, 3]
print(append_to_list(my_list))
print(my_list)
Output
Discover more from Python Mania
Subscribe to get the latest posts sent to your email.