ED5340 - Data Science: Theory at h y
and Practise a p
gan
th u
M u
a n
th
L9 - Functions m a n a
R a
Ramanathan Muthuganapathy (https://ed.iitm.ac.in/~raman)
Course web page: https://ed.iitm.ac.in/~raman/datascience.html
Moodle page: Available at https://courses.iitm.ac.in/
Functions
Assuming you are aware of how functions work in C/C++
def function_name(arg1, arg2…….): #Function definition
h y
statement1 pat
a
gan
th u
u
statement2 a n M
ath
a n
a m
…….. R
return var_name(s)
a1 = …..; a2 = ……; ………
function_name(a1, a2, …….) #Calling the function with passing of arguments
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
Functions
Simple example
def cal_sum(x, y): #Function definition
h y
s = (x + y) pat
a
gan
th u
u
return s a n M
ath
a n
a m
a=5 R
b = 10
su = cal_sum(a, b) #Calling the function with passing of arguments
print(su)
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
Functions
Interaction of arguments
• Positional arguments - order / type has to match
h y
at
• Keyword arguments - order need not match
an
a p
u g
uth
• Using both pos. and kw args. th a n M
n a
m a
R a
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
Demo using
L9_functions.py
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
Functions
Variable length arguments
• Variable length positional arguments - number of args is not fixed
h y
at
• Variable length keyword arguments - number
an
a p of keyword args is not fixed
u g
uth
n M
th a
n a
m a
R a
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
Demo using
L9_functions_var_args.py
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
CW:
(a) Function to find whether a
given number is even or odd
(b) Given a list of integers, do (a)
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
when using both positional and keyword args
• In general, positional args before keyword args
h y
at
• Fixed number of args before variable number
an
a p
u g
uth
• Combining the above two - pos. args,
th a n var. pos. args, kwargs, var. kwargs
M
n a
m a
• Default arguments R a
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
Demo using
L9_functions_combined.py
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
Recursion function
• A function that calls itself from within its body.
h y
at
• Example function an
a p
u g
uth
def PrintNum(n): #Function definition
h a n M
a t
an
m
Ra to the same function
PrintNum(n-1) #Recursive call
PrintNum(10) #calling of the function - this is when the function gets called
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
CW: Code that function.
What are your observation(s)?
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
Recursion function
Modification of the example function
def PrintNum(n): #Function definition
h y
if PrintNum > 0 pat
a
gan
th u
u
print(n) a n M
ath
a n
a m
PrintNum(n-1) #Recursive
R call to the same function
PrintNum(2) #calling of the function - this is when the function gets called
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
Recursion function
How does it work
def PrintNum(n=2): def PrintNum(n=1): def PrintNum(n=0):
if n > 0 if n > 0 a t h y if n > 0
a p
print(2) print(1) gan
th u
PrintNum(1) PrintNum(0)
M u
a n
ath
a n
a m
R
PrintNum(2)
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
Recursion function (Head recursion)
Modification of the example function
def PrintNum(n): #Function definition
h y
if n > 0 pat
a
gan
th u
u
PrintNum(n-1) #Recursive callhato
n the function before other statements
M
a t
an
m
print(n) Ra
PrintNum(2) #calling of the function - this is when the function gets called
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
Recursion function
How does it work
def PrintNum(n=2): def PrintNum(n=1): def PrintNum(n=0):
if n > 0 if n > 0 a t h y if n > 0
a p
PrintNum(1) PrintNum(0) g a n
th u
print(2) print(1)
M u
a n
ath
a n
a m
R
PrintNum(2)
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
Recursion function
Sum of ’n’ values
def sum_n(n):
h y
if n == 0: pat
a
an
return 0 th u g
u
else: a n M
s = n+sum_n(n-1) ath
a n
print(s) a m
R
return s
s = sum_n(2)
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
Recursion function
How does it work
def sum_n(2): def sum_n(1): def sum_n(0):
if n == 0: if n == 0: a t h y if n == 0:
a p
return 0 return 0 gan return 0
th u
else: else: M u else:
a n
s = 2+sum_n(1) san=ath1+sum_n(0) s = 0+sum_n(-1)
m 0
print(s) R print(s)
a print(s)
return s return s return s
1
s = sum_n(2)
print(s) ——output?
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
CW: Do the factorial of any given value ’n’ using recursion
HW: Series summation - odd factorial, even factorial,
exp(x), sin(x), etc. using recursion
HW: Find the sum of digits of a given integer
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
Lambda function
• It does not have a name
h y
at
• used for short functional body an
a p
u g
uth
• can take multiple arguments but returns
th a n only
M one value
n a
am
• also called as `anonymous’ a
function
R
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
Lambda function
syntax and examples
• syntax is
h y
at
• lambda arguments : expression an
a p
u g
uth
• lambda a, b : (a+b) / 2 th a n M
n a
m a
• you can also do the following:
R a
avg = lambda a, b : (a+b) / 2
print(avg(10,30))
print(avg(100,245.6))
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
Demo using
L9_lambda_functions.py
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
Map, filter and reduce functions
Use of lambda function
• Map applies a function to each element in a sequence
h y
at
• Returns a map, needs to be converted toanaap list
u g
uth
• map(function, sequence) th a n M
n a
a m
• filter - filters the value in a a
sequence
R based on a function a, b : (a+b) / 2
• filter(function, sequence)
• reduce - performs a rolling computation to sequential values in a sequence
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras
Demo using
L9_MapFilterReduce.py
Ramanathan Muthuganapathy, Department of Engineering Design, IIT Madras