Exp1 3
Exp1 3
Python Programming
Functions
Outline
• Introduction
• Syntax and Basics of a Function
• Use of a function
• Parameters and Arguments
• Local and Global Scope of a Variable
• Return statement
• Recursive Functions
Introduction
• Syntax of Function
def user( ):
print("Python Programming")
print("This is user defined function")
user( );
Use of a function
• Suppose that you need to find the sum of integers from 1 to 10, 20 to 40 ,
and 50 to 100. If you create a program to add these three sets of numbers,
your code might look like this:
sum = 0
for i in range(1, 11):
sum = sum+i
print("Sum from 1 to 10 is", sum)
sum=0
for i in range(20,41):
sum = sum+i
print("Sum from 20 to 40 is", sum)
sum = 0
for i in range(50, 101):
sum = sum+i
print("Sum from 50 to 100 is", sum)
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)
Use of a function
# Using Function
def sum(x,y):
s=0
for i in range(x,y+1):
s=s+i
print("Sum of values from",x,'to',y,'is',s)
sum(1,10)
sum(20,40)
sum(50,100)
# End of Program
Use of a function
• Functions are reusable code blocks, they only need to be written once,
then they can be used multiple times. They can even be used in other
applications, too.
• The code is usually well organized, easy to maintain and developer
friendly.
• It can support the modular design approach.
• A well-defined and thoughtfully written user-defined function can ease
the application development process.
# Positional Arguments
def display(name,course,per):
print("Your name is:",name,"\nYou are enrolled for:",course,
"\nYour percentage is:",per)
display('abc','BE',81.79)
# End of Program
# Keyword Arguments
def display(name,course,per):
print("Your name is:",name,"\nYou are enrolled for:",course,
"\nYour percentage is:",per)
display(course='BE',per=81.79,name='abc')
Return statement
Return statement
print(result) print(result)
Return statement
Return statement
import math
a= eval(input("Enter value of a= "))
b= eval(input("Enter value of b= "))
c= eval(input("Enter value of c= "))
def roots(a,b,c):
disc = b**2 - 4*a*c
if disc >= 0:
print("Value of a:",a,"Value of b:",b,"Value of c:",c)
print("Discriminant",disc)
return ("r1=",(-b + math.sqrt(disc))/(2*a),"r2=",(-b - math.sqrt(disc))/(2*a))
else:
print("Value of a:",a,"Value of b:",b,"Value of c:",c)
print("Discriminant",disc)
return ('r1=',-b/(2*a),'+i',math.sqrt(disc*(-1))/(2*a),
'r2=',-b/(2*a),'-i',math.sqrt(disc*(-1))/(2*a))
print(roots(a,b,c))
Return statement
Return statement
Recursive Functions
Recursive Functions
• Advantages of Recursion
• Recursive functions make the code look clean and elegant.
• A complex task can be broken down into simpler sub-problems using
recursion.
• Sequence generation is easier with recursion than using some nested
iteration.
• Disadvantages of Recursion
• Sometimes the logic behind recursion is hard to follow through.
• Recursive calls are expensive (inefficient) as they take up a lot of
memory and time.
• Recursive functions are hard to debug.
Recursive Functions
# Factorial of a number
num=int(input("Enter the number:"))
def factorial(n):
if n==0:
return 1
return n*factorial(n-1)
print(factorial(num))
Recursive Functions
Recursive Functions
Programs
THANKS…..