Fuction Theory DS
Fuction Theory DS
Dr. D.P.Sharma,MUJ
HOW TO WRITE and
CALL/INVOKE A FUNCTION
def is_even( i ):
"""
Input: i, a positive int
Returns True if i is even, otherwise False
"""
print("inside is_even")
return i%2 == 0
X=3
is_even(x)
8/11/2020 Python Programming 4
Dr. D.P.Sharma,MUJ
IN THE FUNCTION BODY
def is_even( i ):
"""
Input: i, a positive int
Returns True if i is even, otherwise False
"""
print("inside is_even")
return i%2 == 0
Dr. D.P.Sharma,MUJ
Functions
Dr. D.P.Sharma,MUJ
Various Forms of Function Arguments
There are 4 types of actual arguments are allowed in Python.
1) Positional Arguments
2) Keyword Arguments
3) Default Arguments
Dr. D.P.Sharma,MUJ
Python Positional Arguments --Examples
def greet(name,msg):
Dr. D.P.Sharma,MUJ
Python Default Arguments--Examples
Sometimes we can provide default values for our positional arguments.
can provide a default value to an argument by using the assignment operator (=).
Dr. D.P.Sharma,MUJ
Python Keyword Arguments--Examples
We can pass argument values by
keyword i.e by parameter name. # 2 keyword arguments
def greet(name, msg = "Good morning!"): greet(msg = "How do you do? ",name = "Bruce")
"""
This function greets to the person with # 1 positional, 1 keyword argument
the provided message.
greet("Bruce",msg = "How do you do?").
Dr. D.P.Sharma,MUJ
Python Keyword Arguments--Examples
Here the order of arguments is not important but number of arguments must be matched.
Note: We can use both positional and keyword arguments simultaneously. But first we
have to take positional arguments and then keyword arguments, otherwise we will get
syntax error.
def wish(name,msg):
print("Hello",name,msg)
wish("DPSharma","GoodMorning") Valid
wish("DPSharma",msg="GoodMorning") Valid
wish(name="DPSharma","GoodMorning") Invalid
Dr. D.P.Sharma,MUJ
Python Arbitrary Arguments --Examples
Sometimes, we do not know in advance the number of arguments that will be passed into a
function. Python allows us to handle this kind of situation through function calls with arbitrary
number of arguments.
function definition use an asterisk (*) before the parameter name to denote this kind of argument
def greet(*names):
"""This function greets all
the person in the names tuple."""
greet("Monica","Luke","Steve","John")
Dr. D.P.Sharma,MUJ
Python Arbitrary Arguments --Examples
We can call this function by passing any number of arguments including zero number.
Dr. D.P.Sharma,MUJ
Python Arbitrary Arguments --Examples
Note: After variable length argument, if we are taking any other arguments then we
should provide values as keyword arguments.
Dr. D.P.Sharma,MUJ
Python Arbitrary Arguments --Examples
Note: We can declare keyword variable length arguments also.
For this we have to use **. Ex. def f1(**n):
Dr. D.P.Sharma,MUJ
Python Functions—Case Study
def f(arg1,arg2,arg3=4,arg4=8):
print(arg1,arg2,arg3,arg4)
1) f(3,2) 3 2 4 8
2) f(10,20,30,40) 10 20 30 40
3) f(25,50,arg4=100) 25 50 4 100
4) f(arg4=2,arg1=3,arg2=4) 3 4 4 2
5) f() Invalid
TypeError: f() missing 2 required positional arguments: 'arg1' and 'arg2'
Dr. D.P.Sharma,MUJ