Functions in Python
ge
Block of statements that perform a specific task.
olle
na C
def func_name( param1, param2..) : Function Definition
Ap
#some work
return val
func_name( arg1, arg2 ..) #function call
Functions in Python
lege
ol
Built-in Functions User defined Functions
n
print( )
a C
Ap
len( )
type( )
range( )
Default Parameters
ge
Assigning a default value to parameter, which is used when no argument is passed.
olle
na C
Ap
Let‘s Practice
lege
WAF to print the length of a list. ( list is the parameter)
Col
pna
WAF to print the elements of a list in a single line. ( list is the parameter)
A
WAF to find the factorial of n. (n is the parameter)
WAF to convert USD to INR.
Recursion
lege
ol
When a function calls itself repeatedly.
#prints n to 1 backwards
na C
Ap Base case
Recursion
e
#returns n!
lleg
a Co
Apn
Let‘s Practice
lege
Write a recursive function to calculate the sum of first n natural numbers.
Col
pna
A
Write a recursive function to print all elements in a list.
Hint : use list & index as parameters.