×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python Function Call by Value and Call by Reference

Last Updated : April 24, 2025

There are following types of Python function calls:

  1. Call by value
  2. Call by reference

Call by value

When, we call a function with the values i.e. pass the variables (not their references), the values of the passing arguments cannot be changes inside the function.

Example: Call a Python function using call by value

# call by value 
def change(data):
    data=45
    print("Inside Function :",data)

def main():
    data=20
    print("Before Calling  :",data)
    change(data)
    print("After  Calling  :", data)

if __name__=="__main__":main()

Output

Before Calling  : 20
Inside Function : 45
After  Calling  : 20

Call by reference

When, we call a function with the reference/object, the values of the passing arguments can be changes inside the function.

Example 1: Calling function by passing the object to the class

class mydata:
    def __init__(self):
        self.__data=0
    def setdata(self,value):
        self.__data=value
    def getdata(self):
        return self.__data

def change(data):
    data.setdata(45)
    print("Inside Function :",data.getdata())

def main():
    data=mydata()
    data.setdata(20)
    print("Before Calling  :",data.getdata())
    change(data)
    print("After  Calling  :", data.getdata())

if __name__=="__main__":main()

Output

Before Calling  : 20
Inside Function : 45
After  Calling  : 45

Example 2: Calling function by passing the reference to the list

def change(data):
    data[1]=25
    print("Inside Function :",data)

def main():
    data=[10,20,30,40,50]
    print("Before Calling  :",data)
    change(data)
    print("After  Calling  :", data)

if __name__=="__main__":main()

Output

Before Calling  : [10, 20, 30, 40, 50]
Inside Function : [10, 25, 30, 40, 50]
After  Calling  : [10, 25, 30, 40, 50]

Call by Value vs Call by Reference

In Python, call by value means passing immutable objects where changes don’t affect the original, whereas call by reference means passing mutable objects where changes can modify the original data.

Python Function Call by Value and Reference Exercise

Select the correct option to complete each statement about function call by value and reference in Python.

  1. In Python, when you pass an immutable object (e.g., int, string) to a function, it is passed by ___.
  2. In Python, when you pass a mutable object (e.g., list, dictionary) to a function, it is passed by ___.
  3. When you modify a mutable object inside a function, the changes are reflected outside the function because Python uses ___ for mutable types.

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.