Python Tutorial – Understand Call By Reference | Detailed Guide with Examples

Here in this Python Tutorial, we will understand Call by reference function can be done even though passing the variable as a reference to give input to the function so that we can perform our operations on that variable in Python Programming.

Call by reference
Calling Function

What Are Function In Python

Functions are the set of a few lines of code that needs to be executed on calling that function and perform a specific task for which that function is being designed so that same, And return a useful answer which might also be used as input for other function or any other operations.

In other words, we can say the function has a specific task to perform on given input in a project or program for which it is defined.

Take an example of performing a certain task that needs an intermediate step to be done for completing it, Or we can say before finding the end result another task need to be done in that case function comes into the picture.

Take a simple example of finding the area of the given figure in meters but the given input is in centimeters so our first task would be converting that cm to meters and that task can be done by a function that is designed for that task only.

Types of Function On The Basis Of Calling

We can then function Simply and by giving arguments also.

Simple Calling of Function

When we just need to all the functions without giving any input (Reference) while calling then we use this type of function here we just name of function <Name_of_Functio>() along with blank brackets to show that we are calling the function.

In such a function, we use a global variable to perform certain actions and get modify or perform certain actions on it.

Calling a Function By Reference of Variable

They are the Function where we provide variables as a reference to perform certain actions or calculations and return the desired answer for which the function is designed.

Here are different types of calls by reference on the basis of the number of arguments given in the functions. We can use a number of arguments to differentiate the different functions which we are calling in case of overloading of function.

In such cases, the number of arguments differentiates which function is being called, And sometimes we differentiate the type of data passed in an argument in Function.

For example, there are 3 functions with the same names and 2 functions with 3 -3 arguments, function 1 like fun(int, int, int) and function2 with like fun(float, float, float), and one with 4 arguments than when we pass 4 arguments in calling then the third function will be called,

and if we need to call function 1 will be calling it when we provide three integers in arguments and for calling function 2 we need to pass three float values as arguments in function calling.

For example

def myFunc(myList):
 print("List received: ",myList)
 myList.append(3)
 myList.extend([7,1])
 print("List after adding some elements:", myList)
 myList.remove(7)
 print("List within called function:", myList)
 return
 
List1=[1]
print("List before function call :",List1)

Output: Call by Reference

Here we can see that how the values of list within the function changed from [1] to [1, 3, 1] and the changes got reflected outside the function.

 

 

Learn more about Function HereĀ Functions in Python

And read more here –https://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference

Leave a Comment

%d bloggers like this: