Discover More about Function calls in Python on Day 18
Table of contents
Introduction :
Welcome back to my Python journey! Yesterday, I laid the foundation with control structures in python.
Today, I dove into Function calling, Let's explore what I learned!
Function
python does not support call by value mechanism
python only works according to call by reference mechanism.
in python call by ref mechanism can behave in two different ways,
call by reference as call by reference
by modifying an object if the location / id / address / reference is same.
list object as parameter ,set object ,dictionary object
call by reference as call by value
by modifying an object if the location / id / address / ref is different
int object as parameter ,float object ,complex object ,str object ,tuple object
int object as parameter
def bbsr ( fp ) :
print ( 'info of formal param before modification : ', fp , id ( fp ))
# modify fp
fp = fp + 10
print ( 'info of formal param after modification : ', fp , id ( fp ))
return fp
def main () :
ap = 10
print ( 'info of actual param : ' , ap , id ( ap ) )
ap = bbsr ( ap )
print ( 'info of actual param : ' , ap , id ( ap ) )
main ()
list object as parameter
def bbsr ( fp ) :
print ( 'info of formal param before modification : ', fp , id ( fp ))
fp . append ( 40 )
fp . append ( 50 )
print ( 'info of formal param aftre modification : ', fp , id ( fp ))
def main () :
ap = [ 10, 20, 30 ]
print ( 'info of actual param : ' , ap , id ( ap ) )
bbsr ( ap )
print ( 'info of actual param : ' , ap , id ( ap ) )
main ()
string object as parameter
def bbsr ( fp ) :
print ( 'info of formal param : ', fp , id ( fp ) )
fp = fp . upper ()
print ( 'fp : ' , fp )
return fp
def main () :
ap = "india"
print ( 'info of actual param : ' , ap , id ( ap ) )
ap = bbsr ( ap )
print ( 'ap : ', ap )
main ()
return statement
. to affect the actual param through formal param in call by value return statement will be applicable.
. return statement is used to terminate a function. But break statement is used to terminate a loop.
. return statement is not allowed without function.
. we can use multiple return statement in a function.
LIMITATION :
return statement can not return more then one values in c, cpp, java.
the default return type of a function in python is None.
. in python return statement can return more then one value as tuple object.
. return statement in python can return N no of values also N types of values.
. PACKING
- box = 10, 20, 30
. UNPACKING
by using index
x = box [ 0 ]
y = box [ 1 ]
z = box [ 2 ]
without using index
x , y , z = box
def bbsr () :
print ( 'hi hi ')
return 10, 20.12 , 1+2j , "india", [1,2,3]
return 100
print ( 'bye bye ')
def main () :
k = bbsr ()
print ( k , type ( k ) )
box = bbsr ()
a , b , c , d , e = box
print ( box , type ( box ))
print ( a , type ( a ) , b , type ( b ), c , d , e )
a , b , c , d , e = bbsr ()
print ( a , type ( a ) , b , type ( b ), c , d , e , type ( e ) )
main ()
Resources
Official Python Documentation: Functions
W3Schools' Python Tutorial: Functions
Scaler's Python Course: Functions
Goals for Tomorrow :
Explore something more on Functions.
what is Anonymous Function.
Conclusion :
Day 18’s a success!
What are your favorite Python resources? Share in the comments below.
Connect with me :
GitHub: [ github.com/p-archana1 ]
LinkedIn : [ linkedin.com/in/archana-prusty-4aa0b827a ]
Join the conversation :
Share your own learning experiences or ask questions in the comments.
HAPPY LEARNING!!
THANK YOU!!
Subscribe to my newsletter
Read articles from Archana Prusty directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Archana Prusty
Archana Prusty
I'm Archana, pursuing Graduation in Information technology and Management. I'm a fresher with expertise in Python programming. I'm excited to apply my skills in AI/ML learning , Python, Java and web development. Looking forward to collaborating and learning from industry experts.