A Function groups related statements that are executed together to perform a specific task. To define a function, we use the keyword 'def
' followed by the name of the function and then parentheses that may include relevant parameters.
In the above example:
- We defined a function called
'add'
. - The function specifies two parameters i.e
a
andb
that should be replaced with actual arguments when the function is being called. - When calling the function
'add'
we passed two arguments10
and20
to match the parametersa
andb
, respectively.
That is typically how arguments works in Python i.e the parameters are specified in the function's declarations, then the actual values are passed as arguments when the function is being called.
The difference between parameters and arguments
Sometimes, the terms 'parameter' and 'argument' are used interchangeably when dealing with functions. However, there is a clear distinction between the two which may not be very apparent.
The main difference between parameters and arguments is that parameters are used in function declaration while arguments are the actual values passed when the function is being called. The following diagram demonstrates this better:
Literarily speaking, parameters are simply placeholders for the values that will be passed as arguments when the function is called.
Types of arguments
There are two type of arguments in Python i.e Positional Arguments and Keyword Arguments.
Positional arguments
Positional arguments are arguments passed to a function in a specific order, based on their position in the function call.
Note that in the above example:
- values 3 and 4 are passed as positional arguments when calling the function.
- The values are automatically assigned to parameters a and b depending on their posltion in the function call.
Similarly, in the above example, argument 10
is assigned to parameter a
, 3
to parameter b
and 7
to parameter c
.
As you have noted above, in positional arguments, the order of the arguments is important as each arguments is assigned to the parameters that matches its posltion i.e The first argument is assigned to the first parameter, the second argument to the second parameters and so on.
keyword arguments
Positional arguments, as we have demonstrated above, are passed to the function based on their position in the function call. On the other hand, keyword arguments are passed to the function based on the argument's name. Each keyword argument is assigned to the parameter that matches its name.
Consider the following example:
In the above example, we used keyword arguments when calling the function add()
. Unlike in positional arguments, we are explicitly assigning an argument to a parameter through its name rather than depending on its position.
Since we are specifying explicitly what arguments should be matched to which parameter, the order of the arguments is not important.
positional and keyword arguments in same function call
Python allows positional arguments and keyword arguments to be used in the same function call. However, all positional arguments must come before the keyword arguments.
Note that in the above example,
- The value
2
is passed as a positional argument while values3
and4
are passed as keyword arguments. - The value
2
is automatically assigned to parametera
while values3
and4
are manually assigned to parametersb
andc
, respectively.
It is worth noting that the positional arguments are passed to the first parameters and the keyword arguments can only be assigned to the remaining parameters. If we assign a keyword argument to a parameter that has already been assigned to by a positional argument, a TypeError
will be raised.
In the above example, a TypeError
exception is raised because we are trying to assign to a parameter a
through a keyword argument and it has already been assigned to by the first positional argument.
Functions with default arguments
Sometimes we may need to have defaults arguments such that , if a function is called without a certain argument, the function will use a default value. Python allows us to do this by assigning the default values to the respective parameters at the function's declaration. Consider the following example.
In the above example we defined the function add() with two parameters a and b. The parameter b is given a default value of 10 so that if the value is not overridden when the function is being called, the default value is used.
Arbitrary number of arguments
Sometimes it not may not be known in advance how many arguments will be passed to a function. For example consider the case with the builtin print()
function, the function can take an arbitrary number of positional arguments each representing a value to be printed to the console.
In the above example, we would have passed more or less values to be printed, the point is, the number of arguments that will be passed is not known in advance until the function is called.
In the next parts we will understand how we can add such a functionality to custom/user-defined function.
Arbitrary positional arguments
To define a function that takes an arbitrary number of arguments, we use an asterisk (*
) before one of the function parameters.
When a function defines a parameter that is preceded by a single asterisk, it effectively supports variable number of positional arguments. By convention, this parameter is usually named "args". The parameter should strictly be defined before all the non-default parameters.
In the above example, we defined two non-default positional parameters a
and b
before defining the parameter with the asterisk to represent any other parameters. You should always remember that the non-default positional parameters should always.
You should not that the arbitrary positional arguments are joined together into a tuple and you can access them just like any other tuple. You can use a for loop to iterate through the tuple to access each item or you can use the index operator to access an individual item.
You cannot have more than one parameter defined with an asterisk, trying this will result in an an error being raised.
Arbitrary keyword arguments
Arbitrary keyword arguments are represented by a parameter that is preceded by two asterisks(**
). All the arbitrary keyword arguments are combined into a dictionary with the parameter name as the dictionary key and the argument's value as the dictionary value.
Just like in positional arguments, you cannot have more than one parameter defined with double asterisks.
To summarize on this, the following order should be followed when specifying parameters and arguments:
- Required Arguments/non-default arguments first
- Default Arguments second
- Variable-length positional arguments (
*args
) third - Keyword Arguments (
**kwargs
) last