Programming in Python
(More on Functions)
Dr. Faisal Anwer
Department of Computer Science
Aligarh Muslim University 1
Copyright © Dept of Computer Science, AMU, Aligarh. Permission required for reproduction or display.
Recap
• Introduction to Function
• Syntax of Function
• Function with return value
Contents
• Types of Arguments
• Positional arguments
• Keyword arguments
• Default arguments
• Variable-length arguments
• Exercises
Types of Arguments
• Arguments
• Arguments are the data you pass, when a method is called.
• Argument is the actual value that gets passed to function.
• Parameters
• A parameter is a variable in a method definition.
•. Arguments can be divided into different categories.
• Positional arguments
• Keyword arguments
• Default arguments
• Variable-length arguments
Positional Arguments
• Positional arguments passing is a method of passing values to
parameters in a function using the convention of “position” of the
parameters to the called function.
• The positional arguments must be passed to a function in correct
positional order.
• The number and position (order) of arguments are important.
• When we call a function with some values (arguments), these values
get assigned to the parameters according to their position.
• The first parameter gets the first value sent.
• The second parameter gets the second value sent and so on.
Positional Arguments: Example
def NewFunction( name, age, quali):
print(“The age of”, name, ‘is’, age)
print(“The qualification of”, name, ‘is’, quali)
NewFunction( “Samar”, 30, “Phd” )
NewFunction( 6, ‘Samar’, “II” )
NewFunction( 4, 20, 4)
• What are the parameters and arguments in the above
example?
Keyword Arguments
•Python allows passing arguments as keyword arguments.
•The order (position) of the arguments doesn’t matter.
•When you use keyword arguments in a function call, the caller
identifies the arguments by the parameter name.
•The keyword arguments may follow positional arguments. (not
vice-versa)
Keyword Arguments: Example
def NewFunction( name, age, qualification):
print("The age of", name, 'is', age)
print("The qualification of", name, 'is', qualification)
#Same as positional arguments
NewFunction( name =‘Sahil', age= 30, qualification='Phd' )
#Different than positional arguments
NewFunction( age=12, name=‘Sahil', qualification= 'V' )
#The keyword arguments may follow positional arguments but
not vice-versa.
NewFunction(‘Sahil', qualification ='Phd', age =30 )
Default Arguments
•Python allows default arguments in the function definition.
•using the assignment operator (=) at the time of function
definition.
•A default argument assumes a default value if a value is not
provided in the function call for that argument.
•If a value is sent, it will overwrite the default value.
•Any number of arguments in a function call can be default
arguments.
•But all arguments to the right side of any default argument must be
the default.
•In the function definition, the positional arguments can not follow
default arguments.
Default Arguments: Example
def boxVolume( length=1, width =1, height=1):
print('\n\n\nlength=', length)
print('width=', width)
print('height=', height)
return length * width * height
print('The default box volume is:', boxVolume())
print('The volume of a box with length 10 is:', boxVolume(10))
print('The volume of a box with length=10, width=5, & height=1 is:',
boxVolume(10, 5))
print('The volume of a box with length=10, width=5 and height=2 is:',
boxVolume(10,5,2))
print('The volume of a box with width 20 is:', boxVolume(10, width=20))
Variable-length arguments
• It may be required to process a function for more arguments than
specified while defining the function.
• These arguments are called variable-length arguments and are
not named in the function definition, unlike formal arguments
(positional, keyword, and default arguments).
• The general syntax for a function with non-keyword variable
arguments is this:
def functionname(formal_args, *var_args_tuple ):
statements
return [expression]
Variable-length arguments
• An asterisk (*) is placed before the variable name that will hold
the values of all variable arguments in the tuple.
• This tuple remains empty if no additional arguments are
specified during the function call.
def Test( arg1, *vartuple ): This would produce the
"This is test" following result:
print("Output is: ", arg1) Output is: 10
Output is: 70
for var in vartuple: 60
print(var) 50
return; Output is: 2.3
Anil
Test( 10 ); Moin
Test( 70, 60, 50 ); 2
[1, 2, 3]
Test(2.3, ‘Anil', ‘Moin', 2, [1, 2, 3])
Arbitrary keyword Arguments
• Python can accept multiple keyword arguments, better
known as **kwargs.
• It stores the arguments in a dictionary instead of tuples.
• The general syntax is:
def functionname(formal_args, **Kwargs):
statements
return [expression]
• By using a dictionary, **kwargs can preserve the names of
the arguments, but it would not be able to keep their
position.
• Since **kwargs is a dictionary, you can iterate over them
Arbitrary keyword Arguments
(Examples)
def Test( arg1, **kwargs ):
print('The arg1 is', arg1)
print(kwargs)
Test( 10 );
Test(10, Agra='Taj Mahal', Delhi='Red Fort’);
def Test( arg1, **kwargs ):
print('The arg1 is', arg1)
for k,v in kwargs.items():
print(v, 'belongs to’, k)
Test(10, Agra='Taj Mahal', Delhi='Red Fort');
Exercises
1. Write a function to check whether a number is prime or
not.
2. Write a function to add arbitrary integers.
3. Write a function to receive multiple strings and count
total characters.
Summary
• Positional arguments
• Keyword arguments
• Default arguments
• Variable-length arguments