Unit 4.1 Python Functions
Unit 4.1 Python Functions
Introduction to Python
Subject Expert
Prof. M. V. Tiwari
Types of Functions
sum (10,32)
sum(1.5, 3)
Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera
Return Statement
We can return a result from a function using return statement.
Functions in python can return multiple values also.
e.g. :
def sum (a, b):
c=a+b
return c # It is returning single value
a1 = sum(90, 30)
print(a1) # result = 120
def sum_sub(a,b):
c=a+b
d=a–b
return c, d
a1, a2 = sum_sub(10,20)
print (a1, ‘ ‘, a2) # result = 30, -10
Note:
➢e.g. :
def comb(r, n):
def fact(a):
f=1
for i in range (1, a+1):
f = f*i
return f
c = fact(n)/(fact(r)*fact(n-r))
print(c)
n = int (input(‘enter total number of flavours:’))
r = int (input(‘enter scoops u can add:’))
comb(r, n)
Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera
Parameter Passing :
1. In Python numbers, string, tuples, list or dictionary are sent to functions
by their references.
2. If we pass integers, floats, strings and tuples to a functions and try to
change their value a new object is created in the function with the
modified value because they are immutable.
3. If we are passing list to a function and try to make some change in the
list then the same list is modified because they are mutable.
def f3(plist):
plist[1] = 40
Eg:
def fact(n):
if n == 0:
return result = 1
else:
return result = return n * (n-1)
num = int(input(“Enter the number : ”))
Print(“The factorial of ”, num, “ is : ”, fact(num))
O/p :- for n= 5 => 120
Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera
EXAMPLES
Q1) What is the result ?
define welcome( ):
print(‘Welcome All’)
welcome()
Options :
A) Welcome All B) All
C) Welcome D) Error
Option 2 :
A) (Current, value): B) ( ): C) (current, value) D) ( )
Option 3 :
A) Pass current B) return current C) return D) pass
Which code segments should you use for line 01 and line 04. Select two
Options:
A)01. def get_name() B) 01. def get_name(name):
c)04. def calc_calories(): D) 04. def calc_calories(miles, calories_per_mile)
Options :
A. 30 B. 0
30 30
C. Error D. 30
Options :
C) Amit D) Error
Options:
A) Hellohello C) hellohellohellohello
B) Error D) hello4
Options :
A)5 10 B) 5 5 10
C) 5 5 20 D) Error
For each of the following statements, select Yes if the statement is true. Otherwise, select No.
Q I) To meet the requirements, line 01 must be changed to the following:
def increment_score(score, bonus, points = 1):
A) Yes B) No
QII) Once any parameter is defined with a default value, any parameters to the right must also be defined with
default values.
A) Yes B) No
Prof. M. V. Tiwari, Dept. of Electronics & Telecommunication Engineering, PRMIT&R, Badnera
Q III) If the function is called with only two parameters, the value of
the third parameter will be None.
A) Yes B) No
Q 9) What is the result ?
def mod(m):
m = m+1 Options :
print(m) A) 10 10
retun m B) 11 10
n = 10 C) 10 11
mod(n) D) 11 11
print(n)
x = 10 Options :
def f1(x): A) 10 2 10
x=2 B) 10 2 2
print(x, end = ‘ ‘) C) 2 10 10
print(x, end = ‘ ‘) D) 2 2 2
f1(x)
print(x, end = ‘ ‘)