Functions
Functions
Function
A function is a set of statements .
Advantage-
Instead of writing the same code again and again for different
inputs, we can call the function.
import math
a=math.sqrt(9)
b=math.pow(4,2)
Function Call
say_hello()
function name
Made by Saurabh Arora
Example - 2
Function Definition
parameters 2 inputs are given to
function and function
is not returning any
def add(a,b): thing
c=a+b
print(c)
Function Call
add(5,8)
Arguments
Made by Saurabh Arora
Example - 3
parameters
x=multiply(5,8)
print(x)
Arguments
Example -
x=50
def test ( ):
print (“Inside test x is” , x)
print (“Value of x is” , x )
test()
Output -
Value of x is 50
Inside test x is 50
Made by Saurabh Arora
When global variable x is modified inside the function
Example -
x=10
def test(): global keyword should
global x be used , otherwise error
x=x+8
print ("Inside test x is" , x)
print ("Value of x is" , x ) We can only access the global
test() variable inside any function but we
print ("Value of x is" , x ) cannot modify it inside any
function.
Output –
For modifying a global variable you
should use global keyword.
Value of x is 10
Inside test x is 18
Value of x is 18
Made by Saurabh Arora
Local Scope
Output –
x= 10
y= 20
x= 10
Traceback (most recent call last):
File "C:/Python34/zz.py", line 8, in <module>
print("y=",y)
NameError: name 'y' is not defined
Made by Saurabh Arora
Most Important - 1
def f():
Local Variable and
s = "I Love London!" Global Variable both
print(s) #local variable have same name
def f():
global s
Global keyword tells
print(s) python to treat the
s = "Inside Function" variable as global. Here
print(s) s variable that is used
inside function is global
s = "Outside Function" variable.
f()
print(s)
Output –
Outside Function
Inside Function
Inside Function
def simple_interest(p,r,t):
print("p=",p)
print("r=",r)
print("t=",t)
si=p*r*t/100
return si
The value for that variable which has no default argument , should
always be sent from function call to function definition like the value
for p is being sent through function call in below example
def simple_interest(p,r=8,t=2):
print("p=",p) Default
print("r=",r) Arguments are
print("t=",t) always
si=p*r*t/100 assigned from
return si right to left
x=simple_interest(1000,5,1)
x=simple_interest(1000,5)
x=simple_interest(1000) Wrong because p does not
x=simple_interest() have a default argument in
function definition
Made by Saurabh Arora
Keyword (Named) Arguments in a Function
The keyword arguments are arguments in which the
parameters names are given with their values in
function call statement
def simple_interest(p,r,t):
print("p=",p)
print("r=",r)
print("t=",t)
si=p*r*t/100
return si Correct, because
ordering does not
x=simple_interest(p=1000,r=5,t=1) matter here
x=simple_interest(t=1,r=5,p=1000)
Made by Saurabh Arora
Combining Default , Positional and Keyword
Arguments
def simple_interest(p,r,t=2):
print("p=",p)
print("r=",r) Correct, because p is positional
print("t=",t) argument, r is keyword argument,
si=p*r*t/100 t is default argument.
return si
Error because positional
x=simple_interest(10000,r=8) arguments is not on the left
of keyword argument.
x=simple_interest(r=8,10000)
Error because multiple values
for p are being sent to
x=simple_interest(10000,p=10000) function
Made by Saurabh Arora
random module
>>>import random
It will return a
>>>random.randint(1,6) value x such that
5 1<=x<=6
import random
print(random.randint(0,5)+20,end=":")
print(random.randint(0,5)+20,end=":")
print(random.randint(0,5)+20,end=":")
print(random.randint(0,5)+20)
i) 20:21:22:23
ii) 19:25:25:22
iii) 22:23:26:24
iv) 23:23:23:23
Made by Saurabh Arora
Practice Questions
Ques. Study the following program and select the possible output(s) from the
options (i) to (iv) following it. Also, write the maximum and the minimum
values that can be assigned to the variable Temp during the overall execution
of program.
import random
Series=[0,2,4,6]
for Count in range(0,4):
Temp = random.randint( Series[Count], Count*3)
print(Temp,":",end="")
i) 0:0:4:6
ii) 0:2:5:7
iii) 0:3:7:8
iv) 0:4:6:9
Made by Saurabh Arora
Practice Questions
Ques. What possible outputs(s) are expected to be displayed on screen at the time
of execution of the program from the following code? Also specify the maximum
values that can be assigned to each of the variables FROM and TO.
import random
AR=[20,30,40,50,60,70];
FROM=random.randint(1,3)
TO=random.randint(2,4)
for K in range(FROM,TO+1):
print (AR[K],end=“#”)
import random
print(int(random.random()*5+20),end=" ")
print(int(random.random()*5+20),end=" ")
print(int(random.random()*5+20),end=" ")
print(int(random.random()*5+20),end=" ")
i) 20 21 22 23
ii) 20 25 25 22
iii) 22 23 25 24
iv) 23 23 23 23
Made by Saurabh Arora
random module
>>>import random
>>>random.randrange(100,150,3)
100
>>>random.randrange(100,150,3)
103
>>>random.randrange(100,150,3)
130
>>>random.randrange(100,150,3)
148
Made by Saurabh Arora
math Module
def increment(n):
n=n+1
print("n=",n)
a=3
increment(a)
print("a=",a)
def increment(n):
n=n+1
print("n=",n)
return n
a=3
a=increment(a)
print("a=",a)
Output:
n= 4
a= 4
Made by Saurabh Arora
Passing Mutable Type to functions
def increment(n):
n.append(4)
L=[1,2,3]
increment(L)
print("L=",L) The List variable L and the
variable n refer to the same list
and the changes done in n
Output: variable will also be seen in L
variable.
L= [1, 2, 3, 4]
def func(x):
return x,x+1,x+2,x+3
Output:
(1, 2, 3, 4)
def func(x):
return x,x+1,x+2,x+3