Functions PDF
Functions PDF
Output:
Welcome to the Concepts of Functions
Ex: Write a program to prompt the name of a user
and print welcome message “Dear name of user,
Welcome to Python Programming.
Use of a Function
• Write a program to add the sum of digits from 1 to 25, 50 to 75 and
90 to 101 using different loop.
Conventional Method: Using Function
Parameters, Arguments in a
Function
def printMax(num1,num2):
Statemen1
Statemen2 #Define a Function
………………………
………………………
StatementN
def FindMin(num1,num2):
if num1 < num2:
print(num1, 'is smaller than ',num2)
elif num2 < num1:
print(num2,' is smaller than ', num1)
else:
print(' Both are equal')
FindMin(20,40)
Output:
20 is smaller than 40
Ex: Write a program to calculate the factorial
of a number using function.
Positional Arguments
Consider the following:
If there are more than one parameters, how does the Python knows
which argument in the function call statement has to be assigned to
which parameter.
Answer:
Parameters are assigned by default according to their positions i.e., the
1st argument in the call statement is assigned to the first parameter
listed in the function definition and the second argument in the call
statement is assigned to the second parameter listed in the function
definition and so on.
Ex: Positional Arguments
def Display(Name,age):
print("Name = ",Name,"age = ",age)
Display("John",25)
Display(40,"Sachin")
Output:
Name = John age = 25
Name = 40 age = Sachin
Thus, the first argument binds to the first parameter and second
argument binds to the second parameter. This style of matching up
arguments and parameter is called “positional argument style” or
“positional parameter style”.
Ex: Positional Arguments
def Display(Name,age):
print("Name = ",Name,"age = ",age)
Display("John“)
Output:
Error
def Display(Name,age):
print("Name = ",Name,"age = ",age)
Display(age=25,Name="John") #Call function using keyword arguments
Output:
Name = John age = 25
Precautions while using Keyword Arguments
•
Output:
Hello Rahul Welcome to Python!!
Parameters with Default Values
def greet(name,msg="Welcome to Python!!"):
print(" Hello ",name, msg)
greet(“Rahul")
Output:
Hello Rahul Welcome to Python!!
Test case 1:
greet(“amit”)
Test case 2:
greet (“Raj”, “How are you”)
Parameters with Default Values
Note:
In function definition, any number of arguments can have default
values, but once we have a default value to a parameter, all the
parameters to its right must also have default values.
Write a program to calculate the area of a circle. Declare the default parameter
value of pi as 3.142 and radius as 1.
The return statement
• The return statement is used to return a value from the
function.
Output:
8 is minimum
Returning Multiple Values
• In python yes it is possible to return multiple values.
• Syntax to return multiple values is as follows
return Value1,Value2,Value3
import math
def Sq_Cub_Srt(a,b,c):
return a**2, b**3, math.sqrt(c)
S,C,Sq = Sq_Cub_Srt(2,3,4)
print('Square = ',S)
print('Cube = ',C)
print('Cube = ',Sq)
Output
Square = 4
Cube = 27
Cube = 2.0
Variable length non-keyword (NKW) arguments
• The variable length argument allows a function to pass or accept “n” i.e
arbitrary no. of arguments.
• The arguments within the function are not named explicitly in the function
declaration because during the execution the no. of arguments may be
different on successive function calls.
• With * operator, a function can pass accept or pass NKW arguments.
Syntax:
def function_name(*args):
………………..
………………..
Function body
………………..
………………
Variable length non-keyword (NKW)
arguments
def calc(*args):
s=0
print(“the numbers are as follows”)
for num in args:
print(num,end=“ “)
s=s+num
return s
______________________________________
Total = calc(10,20)
Print(“sum=“,total)