Call by value and call by reference in python definition

Python utilizes a system, which is known as “Call by Object Reference” or “Call by assignment”. In the event that you pass arguments like whole numbers, strings or tuples to a function, the passing is like call-by-value because you can not change the value of the immutable objects being passed to the function. Whereas passing mutable objects can be considered as call by reference because when their values are changed inside the function, then it will also be reflected outside the function.
Example 1: 
 

Python3

string = "Geeks"

def test[string]:

    string = "GeeksforGeeks"

    print["Inside Function:", string]

test[string]

print["Outside Function:", string]

Output 
 

Inside Function: GeeksforGeeks
Outside Function: Geeks

Example 2 
 

Python3

def add_more[list]:

    list.append[50]

    print["Inside Function", list]

mylist = [10,20,30,40]

add_more[mylist]

print["Outside Function:", mylist]

Output 
 

Inside Function [10, 20, 30, 40, 50]
Outside Function: [10, 20, 30, 40, 50]

Binding Names to Objects

In python, each variable to which we assign a value/container is treated as an object. When we are assigning a value to a variable, we are actually binding a name to an object.
 

Python3

a = "first"

b = "first"

print[id[a]]

print[id[b]]

print[a is b]

Output 
 

110001234557894
110001234557894
True

Now, let’s try and understand this better with another example.
Example 2:
 

Python3

a = [10, 20, 30]

b = [10, 20, 30]

print[id[a]]

print[id[b]]

print[a is b]

Output 
 

541190289536222
541190288737777
False

The output of the above two examples are different because the list is mutable and the string is immutable. An immutable variable cannot be changed once created. If we wish to change an immutable variable, such as a string, we must create a new instance and bind the variable to the new instance. Whereas, mutable variable can be changed in place.
Example 3: 
 

Python3

def foo[a]:

    a = "new value"

    print["Inside Function:", a]

string = "old value"

foo[string]

print["Outside Function:", string]

Output:
 

Inside Function: new value
Outside Function: old value

In the above example, a string which is an immutable type of object is passed as argument to the function foo. Within the scope of the given function foo, a= “new value” has been bounded to the same object that string has been bound outside. Within the scope of the function foo, we modify “old value”` to “new value”. Once we leave the scope of function foo , a=”new value” is no longer in the name space, and the value that string refers to was never changed.
Example 4: Now, let us look at how mutable variable is passed into the function.
 

Python3

def foo[a]:

    a[0] = "Nothing"

bar = ['Hi', 'how', 'are', 'you', 'doing']

foo[bar]

print[bar]

Output: 
 

['Nothing, 'how', 'are', 'you', 'doing']

When we pass a mutable variable into the function foo and modify it to some other name the function foo still points to that object and continue to point to that object during its execution. 
 


What is meant by call by value and call by reference?

While calling a function, we pass values of variables to it. Such functions are known as “Call By Values”. While calling a function, instead of passing the values of variables, we pass address of variables[location of variables] to the function known as “Call By References.

What is call by value with example in Python?

Call by Value in Python When Immutable objects such as whole numbers, strings, etc are passed as arguments to the function call, it can be considered as Call by Value. This is because when the values are modified within the function, then the changes do not get reflected outside the function.

What is call by value?

The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. By default, C programming uses call by value to pass arguments.

What is meant by call by reference?

The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means the changes made to the parameter affect the passed argument.

Chủ Đề