Difference between call by value and call by reference in Python

Difference between call by value and call by reference in Python : In this tutorial, we will study the call by value and call by reference in python programming. Python programming language uses the mechanism of the call-by-object and also call by object reference. If you pass the arguments like strings, tuples to function the passing values will act as the call by value.

difference between call by value and call by reference in Python

Functions are used in two ways as:-

Explanation of call by value:-
This evaluation is called as the pass by value and also used in other languages.
The argument is evaluated and the result is bound to the variable in function.
The local copy of the value will be used as the scope will be unchanged and the function is returned.
Explanation of call by reference:-
The call is evaluated and known as the passed value by reference and the function gets implicit to the argument.
The function can modify argument of variables and changes.
The advantage of using call by reference is needed greater time and good efficiency.
The disadvantage us the variables get changed in the function call.

Value passing:-

The python language will use a mechanism known as the call by object and called the call by sharing method.
Whenever you pass the argument like string, tuple to function the passing will act as the call by value and if we pass the mutable arguments then it is the call by reference.
The arguments in the language are passed by the reference and if you change parameter within the function the change is also reflected back in calling function.

Difference between call by value and call by reference in Python

Call by value

Call by reference

Call by value:-The parameter values are copied to function parameter and then two types of parameters are stored in the memory locations.
When changes are made the functions are not reflected in the parameter of the caller.

Call by reference:-
The actual, as well as the formal parameters refer to the same location and if changes are made inside the functions the functions are reflected in the actual parameter of the caller.

While calling the function we pass values and called as call by values.

At the time of calling instead of passing the values we pass the address of the variables that means the location of variables so called call by reference.

The value of each variable is calling the function and copy variable into the variables of the function.

The address of variable in function while calling is copied into dummy variables of a function.

The changes are made to dummy variables in the called function that have no effect on the actual variable of the function.

By using the address we have the access to the actual variable.

We cannot change the values of the actual variable through a function call.

We can change the values of the actual variable through a function call.

The values are passed by the simple techniques

The pointers are necessary to define and store the address of variables.

Call by value Example:-

def primtme (str):
print str
return;
printme(“I’m first call to user defined function!”)
printme(“Again second call to same function”)
Output:-
I’m first call to user defined function!
Again second call to same function

Call by reference:-

def changeme(mylist):
mylist.append(1, 2, 3, 4]);
print”values inside the function:” mylist
return
mylist= [10, 20, 30];
changeme (mylist);
print”values outside the function:” mylist
Output:-
values inside the function: [10, 20, 30, [1, 2, 3, 4]]
values outside the function: [10, 20, 30, [1, 2, 3, 4]]

Additional Services : Refurbished Laptops Sales, Python Classes, Share Market Classes And SEO Freelancer in Pune, India