4 - Functions in Python
4 - Functions in Python
12/4/2021 1
Functions
• A function is a block of code which only runs when it is called.
• You can pass data, known as parameters, into a function.
• A function can return data as a result.
• The idea is, to put some commonly or repeatedly done task
together and make a function, so that instead of writing the same
code again and again for different inputs, we can call the function.
• Functions provide better modularity for your application and a high
degree of code reusing.
• As our program grows larger and larger, functions make it more
organized and manageable.
12/4/2021 2
Syntax to write function
def function_name(parameters):
"""docstring"""
statement(s)
12/4/2021 3
Defining and Calling a function
• Creating / Defining Function
– In Python a function is defined using the def keyword Jump on
the
def my_function(): function
print("Hello from a function") call
def my_function():
12/4/2021 4
Parameters
• Information can be passed to functions as parameter.
• Parameters are specified after the function name, inside the parentheses. You can
add as many parameters as you want, just separate them with a comma.
def evenOdd( x ):
# Function Call
if (x % 2 == 0):
evenOdd(3)
print "even"
evenOdd(4)
else:
print "odd" odd
even
12/4/2021 5
Passing Parameters
12/4/2021 6
Call by value/Call by reference
12/4/2021 7
Call by value / call by reference
• Changes made to a parameter value within the function do not
affect the argument
– Known as pass by value
– Provides a way for unidirectional communication between one
function and another function
• Calling function can communicate with called function
12/4/2021 8
Call by value / call by reference
12/4/2021 9
Default Parameter Value
• def my_function(country = "Norway"):
print("I am from " + country)
my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")
• ___________________________
• I am from Sweden
I am from India
I am from Norway
I am from Brazil
12/4/2021 10
Passing a List as a Parameter
• def my_function(food):
for x in food:
print(x)
fruits = ["apple", "banana", "cherry"]
my_function(fruits)
• ___________________________________
• apple
banana
cherry
12/4/2021 11
Return Values
• def my_function(x):
• return 5 * x
• print(my_function(3))
• print(my_function(5))
• print(my_function(9))
• _______________________________________
• 15
25
45
12/4/2021 12
Recursion
• Python also accepts function • def tri_recursion(k):
• if(k>0): Recursion
recursion, which means a Example
• result = k+tri_recursion(k-1) Results
defined function can call itself. • print(result) 1
• Recursion is a common • else: 3
6
mathematical and programming • result = 0 10
concept. It means that a function • return result 15
21
calls itself. This has the benefit of
• print("\n\nRecursion Example Results")
meaning that you can loop • tri_recursion(6)
through data to reach a result.
12/4/2021 13
Try
• WAP for Fibonacci series • WAP to find factorial of a
number.
12/4/2021 14
Lambda
• A lambda function is a small anonymous • x = lambda a: a + 10
function. • print(x(5))
• A lambda function can take any number of • ________________
arguments, but can only have one • 15
expression.
• lambda arguments : expression
• def myfunc(n):
• return lambda a : a * n
• mydoubler = myfunc(2)
• print(mydoubler(11))
• ________________________
• 22
12/4/2021 15
Programs
1. Python program to remove Nth occurrence of the given word.
Input: list - ["can", "you", "can", "a", "can" "?"]
word = can, N = 1
Output: list - ["you", "can", "a", "can" "?"]
2. Python program for Reversing a List.
3. Program to display the Fibonacci sequence up to n-th term where
n is provided by the user (with using recursion)
4. WAP to count no of vowels in a string .
5. Python Program to Check if a String is a Palindrome or Not.
12/4/2021 16
12/4/2021 17
def Reverse(lst):
new_lst = lst[::-1]
return new_lst
12/4/2021 18
# Python code to illustrate cube of a number showing
difference between def() and lambda().
• def cube(y):
• return y*y*y;
•
• g = lambda x: x*x*x
• print(g(7))
•
• print(cube(5))
12/4/2021 19
12/4/2021 20