Quick links
Latest articles
Internship
Members
New...
Algorithms
Discrete Mathematics
Big data
Languages
C
C++
C++ STL
Java
Data Structure
C#.Net
Android
Kotlin
SQL
Web
PHP
Python
JavaScript
CSS
Ajax
Node.js
Web prog.
Programs
C
C++
DS
Java
C#
Python
Aptitude
C
C++
Java
DBMS
Interview
C
Embedded C
Java
SEO
HR
CS Subjects
CS Basics
O.S.
Networks
DBMS
Embedded Systems
Cloud Computing
Machine learning
CS Organizations
Linux
DOS
More...
Articles
Puzzles
News/Updates

Home » Python

Python | Types of function calling with Examples



Python function calling types: Here, we are going to learn about function calling types in python with Examples.
Submitted by Pankaj Singh, on October 11, 2018

There are following types of function calls in python:

  1. Call by value
  2. Call by reference

1) 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.

# 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

2) 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]





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Featured post:
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distribution Software (Distros) of 2018

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.