Python is a high-level, object-oriented programming language that is robust and has many applications. There are many ways to pass arguments to functions, but the most common ones are positional and keyword arguments. Default arguments can also be used with both of them. Learn how they work in this blog post.
Introduction
When invoking a function, you need to pass arguments to parameters. A function’s arguments can be passed as positional arguments or keyword arguments.
The power of a function lies in its ability to work with parameters.
There are two types of arguments: positional arguments and keyword arguments.
Positional Arguments
To use positional arguments, the arguments need to be passed in the same order as their respective parameters in the function definition.
def getgrade(name, score):
""" This function computes a grade given a score"""
if score > 80:
grade = 'A'
elif 80 > score > 70:
grade = 'B'
elif 70 > score > 60:
grade = 'C'
else:
grade = 'D'
return name + " had grade: " + grade
To call this function using positional arguments:
getgrade('prince', 78) # prince had grade: B
The getgrade('prince', 78)
statement automatically passes 'prince'
to the name parameter and 78
to the score parameter.
getgrade(78, 'prince')
This function call is not the same as getgrade('prince', 78)
because this statement passes 78
to name and 'prince'
to score.
And since the score parameter is supposed to be an integer but a string is passed to, it will raise an
error and halt the program.
That's all for positional arguments: note the arguments need to be passed in the same order as their respective parameters in the function definition.
Keyword Arguments
You can also call a function using keyword arguments, passing each argument in the form name = value
.
We are going to be using the same getgrade()
function to demonstrate the use of keyword arguments.
To call the getgrade()
function using keyword arguments:
getgrade(name='prince', score=78) # prince had grade: B
This is very clear, we are assigning 'prince'
to name and 78
to score. The exciting part of using keyword arguments is you can mix the order in which you provide the arguments.
getgrade(score=78, name='prince') # prince had grade: B
The arguments can appear in any order, unlike positional arguments.
Mixing Positional and Keyword arguments
It is possible to mix positional arguments with keyword arguments
But the positional arguments cannot appear after any keyword arguments have been defined.
For example, if you have a function header as:
def f(p1, p2, p3, p4):
You can invoke it by using:
f(21, p2=43, p3=11)
It would be wrong to invoke by:
f(p1=21, 43, 11)
because the positional arguments 43
and 11
appears after the keyword argument p1=21
.
Default Arguments
Python allows us to create functions with parameters that have default values.
The default values are passed to the parameters when the function is invoked without the arguments.
Let's define a function with default arguments.
def area_perimeter(width=2, height=2):
area = width * height
perimeter = (2 * width) + (2 * height)
print("Area = " + str(area) + " and Perimeter = " + str(perimeter))
The width parameter
has a default value of 1 and height
has a value of 2.
If this function is called without one or any of the arguments. it will automatically pass the default values as the arguments.
Various ways to call this function:
area_perimeter() # this function call uses the default arguments
area_perimeter(10, 5) # positional arguments width=10 height=5
area_perimeter(width=10) # this will set height to use the default value, 2
area_perimeter(height=4, width=45) # keyword arguments
area_perimeter(15) # sets width = 15 and height uses the default value
Program Output:
Area = 4 and Perimeter = 8
Area = 50 and Perimeter = 30
Area = 20 and Perimeter = 24
Area = 180 and Perimeter = 98
Area = 30 and Perimeter = 34
The use of Positional arguments, keyword arguments, and default arguments helps to write more efficient functions.
f(21, p2=43, p3=11) doesn't work in 3.9.4. You need to supply keyword arguments for all arguments after and including the earliest supplied keyword argument, according to the sequence in def.