Function
Function
Example 1
def display():
print("Hello")
display()
Output:
Hello
def add(x,y):
r=x+y
print("Result",r)
add(a,b)
Output:
def add(x,y):
s=x+y
return s
r=add(a,b)
print("Result=",r)
Output:
def add(x,y):
s=x+y
return s
addition=add
r=addition(a,b)
print("Result=",r)
Output:
def display():
print("ABCD")
return
print("XYZ")
display()
Output:
ABCD
def display():
print("Hello")
return
display()
print(display())
Output:
Hello
Hello
None
Example 5
def add_sub(x,y):
c=x+y
d=x-y
return c,d
r1,r2=add_sub(a,b)
print("Result of Addition=",r1)
print("Result of Subtraction=",r2)
def swap(x,y):
return y,x
print("Before Swap:")
print("a=",a)
print("b=",b)
a,b=swap(a,b)
print("After Swap:")
print("a=",a)
print("b=",b)
Dr. Shiladitya Chowdhury
Ph.D(Computer Science & Engineering)(JU)
9
Assistant Professor, Dept. of MCA
Techno Main Saltlake, Kolkata 700091
Output:
def update(x):
print("Inside Finction 1 :x=",x,"Address=",id(x))
x=8
print("Inside Finction 2 :x=", x, "Address=", id(x))
x=10
update(x)
print("Outside Finction :x=",x,"Address=",id(x))
Output:
def update(lst):
print("\nIn function before update :")
print("List:", lst)
print("List Address:", id(lst))
print("Address of list elements:")
for i in range(3):
print((id(lst[i])))
lst[1]=25
update(lst)
def update(tup):
print("\nIn function before update :")
print("Tuple:", tup)
print("Tuple Address:", id(tup))
print("Address of Tuple elements:")
for i in range(3):
print((id(tup[i])))
tup[1]=25
update(tup)
Global variables are those variables which are defined in the main body of the
program file.
Example 1
a=10
print("In Global Section: a=",a,"Address=",id(a))
def show():
a=20
print("Inside function: a=", a, "Address=", id(a))
show()
print("Outside function: a=",a,"Address=",id(a))
Dr. Shiladitya Chowdhury
Ph.D(Computer Science & Engineering)(JU)
20
Assistant Professor, Dept. of MCA
Techno Main Saltlake, Kolkata 700091
Output:
a=10
print("In Global Section: a=",a,"Address=",id(a))
def show():
global a
a=20
print("Inside function: a=", a, "Address=", id(a))
show()
print("Outside function: a=",a,"Address=",id(a))
Output:
a=10
print("In Global Section: a=",a,"Address=",id(a))
def show():
global b
b=20
print("Inside function: b=", b, "Address=", id(b))
show()
print("Outside function: a=",a,"Address=",id(a))
print("Outside function: b=", b, "Address=", id(b))
Output:
Example 1
def fun():
print("a=",a)
a=20
fun()
Output:
a= 20
Dr. Shiladitya Chowdhury
Ph.D(Computer Science & Engineering)(JU)
24
Assistant Professor, Dept. of MCA
Techno Main Saltlake, Kolkata 700091
A local variable cannot be defined with the same name as global variable.
To do this we must need global statement.
Example 2
def fun():
print("a=",a)
a=10
print("a=", a)
a=20
fun()
Output:
Traceback (most recent call last):
File "C:/Users/Admin/PycharmProjects/function1/resolutionName_2.py", line 7, in
<module>
fun()
File "C:/Users/Admin/PycharmProjects/function1/resolutionName_2.py", line 2, in fun
print("a=",a) Dr. Shiladitya Chowdhury
Ph.D(Computer
UnboundLocalError: local variable 'a' referenced Science & Engineering)(JU)
before assignment 25
Assistant Professor, Dept. of MCA
Techno Main Saltlake, Kolkata 700091
Example 3
def fun():
global a
print("a=",a)
a=10
print("a=", a)
a=20
fun()
Output:
a= 20
a= 10
In case of nested functions (function inside another function), the inner function
can access variables defined in both outer as well as inner function; but the
outer function can access variables defined only in the outer function.
def outer_fun():
outer_var=10
def inner_fun():
inner_var=20
print("In Inner fun: outer variable=",outer_var)
print("In Inner fun: inner variable=",inner_var)
inner_fun()
print("In Outer fun: outer variable=", outer_var)
print("inner variable=",inner_var)
outer_fun()
def outer_fun():
var=10
def inner_fun():
var=20
print("In Inner fun: var=",var,"Address=",id(var))
inner_fun()
print("In Outer fun: var=", var,"Address=",id(var))
outer_fun()
Output:
Example 1
def display(name,roll,avgmarks):
print("Name:",name)
print("Roll:",roll)
print("Avg Marks:",avgmarks)
display(roll=10,avgmarks=78.25,name="Raja Roy")
Output:
def display(ename,age,salary):
print("Name:",ename)
print("Age:",age)
print("Salary:",salary)
n="Arun Das"
a=35
sal=50000
display(salary=sal,ename=n,age=a)
Output:
display("Raja Roy")
display("Amit Das","BCA")
display("Arup Saha","MCA")
Output:
Name: Raja Roy
Course: B.Tech
Name: Amit Das
Course: BCA
Name: Arup Saha Dr. Shiladitya Chowdhury
Ph.D(Computer Science & Engineering)(JU)
Course: MCA Assistant Professor, Dept. of MCA
32
Techno Main Saltlake, Kolkata 700091
We can specify any number of defaults arguments to a function.
Example 2
def display(name,course="B.Tech",age=18):
print("Name:",name)
print("Course:",course)
print("Age:",age)
display("Raja Roy")
display("Amit Das","BCA")
display("Arup Saha","MCA",21)
Output:
Name: Raja Roy
Course: B.Tech
Age: 18
Name: Amit Das
Course: BCA
Age: 18
Name: Arup Saha
Course: MCA Dr. Shiladitya Chowdhury
Ph.D(Computer Science & Engineering)(JU)
Age: 21 33
Assistant Professor, Dept. of MCA
Techno Main Saltlake, Kolkata 700091
If we have default arguments, then they must be written after the non-default
arguments.
Example 3
def display(name,course="B.Tech",age):
print("Name:",name)
print("Course:",course)
print("Age:",age)
display("Amit Das","BCA",18)
display("Arup Saha","MCA",21)
Output:
C:\Users\Admin\PycharmProjects\function1\venv\Scripts\python.exe
C:/Users/Admin/PycharmProjects/function1/defaultArg3.py
File "C:/Users/Admin/PycharmProjects/function1/defaultArg3.py", line 1
def display(name,course="B.Tech",age):
^ Dr. Shiladitya Chowdhury
SyntaxError: non-default argument follows
Ph.D(Computer default
Science argument
& Engineering)(JU)
34
Assistant Professor, Dept. of MCA
Techno Main Saltlake, Kolkata 700091
Variable-length Arguments
def display(name,*fevsubs):
print("Name:",name,"likes following subjects:")
for i in fevsubs:
print(i)
display("Raja Roy","Mathematics")
display("Amit Das","Mathematics","Biology")
display("Arup Saha","Mathematics","Physics","Chemistry")
display("Arun Dutta","Biology","Mathematics","Physics","Chemistry")
def show(a,*b):
print("a=",a)
print("b:",b)
for i in b:
print(i)
show(5,10,20,30)
show("Arun Dutta","Biology","Mathematics","Physics","Chemistry")
a= 5
b: (10, 20, 30)
10
20
30
a= Arun Dutta
b: ('Biology', 'Mathematics', 'Physics', 'Chemistry')
Biology
Mathematics
Physics
Chemistry
Recursion one can solve problems in easy way while its iterative solution
is very big and complex.
def fact(n):
f=1
if n==0 or n==1:
return 1
else:
return n*fact(n-1)
n=int(input("Enter No:"))
if n>=0:
r=fact(n)
print("Result=",r)
else:
print("Wrong Input")
Enter No:6
Result= 720
Enter No:-4
Wrong Input
Example 1
def add(x,y):
s=x+y
return s
r=add(a,b)
print("Result=",r)
def add(x,y,z):
s=x+y+z
return s Dr. Shiladitya Chowdhury
Ph.D(Computer Science & Engineering)(JU)
43
Assistant Professor, Dept. of MCA
Techno Main Saltlake, Kolkata 700091
a=int(input("Enter 1st No:"))
b=int(input("Enter 2nd No:"))
c=int(input("Enter 3rd No:"))
r=add(a,b,c)
print("Result=",r)