After creating a function with multiple parameters in Python, it is essential to know how to order the arguments for the parameters of the function whenever the function is called.
A parameter in Python is the variable listed inside the parentheses in the function definition. For example, greet_customer(name)
is a function that has (name)
as its parameter.
An argument in Python is the value that is supplied to the parameter whenever the function is called.
The various types of argument in Python are:
Positional arguments are arguments that can be called by their position in the function definition.
Let’s define a function with two parameters. The arguments of the two parameters will be positioned accordingly.
# defining a function with two parametersdef greet_user(first_name, last_name):print(f' Hi {first_name} {last_name} welcome on board! ')# using the corresponding positional argumentsgreet_user('Theophilus', 'Onyejiaku')
In the example above, the positional arguments are 'Theophilus'
and 'Onyejiaku'
, and they are both positioned respectively for the first_name
and last_name
parameters.
Keyword arguments are arguments that are preceded by an identifier (e.g. first_name=
) when calling a function.
Let’s define a function with two parameters. We will call the function with keyword arguments.
# defining a function with two parametersdef greet_user(first_name, last_name):print(f' Hi {first_name} {last_name} welcome on board! ')# using keyword argumentsgreet_user(first_name= 'Theophilus', last_name= 'Onyejiaku' )
In the example above, the keyword arguments are first_name='Theophilus'
and last_name= 'Onyejiaku'
.
Required arguments are arguments that must be passed to the function.
In the previous example, we define a function, greet_user(first_name, last_name)
. Since we never gave the parameters any value when defining the function, we must pass the required argument when calling the function. Hence, the required arguments used in the previous example are Theophilus
and Onyejiaku
.
You can also choose which parameters need to be required.
# defining a function with only one required argument for the parameters# Note that a required argument must come before the default argumentdef greet_user(last_name, first_name = 'Theophilus'):print(f' Hi {first_name} {last_name} welcome on board! ')# using the required arguments onlygreet_user(last_name = 'Onyejiaku' )
We can see that the requirement argument from the program above is last_name
.
A required argument must come before the default argument for the program to be successful.
Optional arguments are arguments that cannot be passed to the function. In Python, optional arguments are arguments that have a default value.
In order of positioning, the positional argument must come first, i.e., before the keyword argument.
Let’s write a code to see which comes first between a positional argument and a keyword argument when calling both in a program.
# defining a function with two parametersdef greet_user(first_name, last_name):print(f' Hi {first_name} {last_name} welcome on board! ')# let the keyword argument come firstgreet_user(first_name='Theophilus', 'Onyejiaku')
In the program above, you can see the error message that reads:
SyntaxError: positional argument follows keyword argument
This happens because, when passing the arguments, our keyword argument comes before the positional argument.
Now, let’s write a new code, but this time, let the positional argument come before the keyword argument.
# defining a function with two parametersdef greet_user(first_name, last_name):print(f' Hi {first_name} {last_name} welcome on board! ')#Let the positional argument come firstgreet_user('Theophilus', last_name = 'Onyejiaku', )
Since the program above runs successfully, we can conclude that when using positional and keyword arguments in Python, the positional argument must come before the keyword argument.