Types of Function Arguments in Python
Video Tutorial
Overview
An argument is the value sent to the function when it is called in Python. Arguments are often confused with parameters, and the main difference between both is that parameter is a variable inside the parenthesis of a function. In contrast, an argument is a value passed to it.
Scope
- In this article, we will learn about function arguments in Python.
- Then, we will look into different types of arguments that we could use with functions and try to understand them all with the help of suitable examples and explanations.
What are Function Arguments in Python?
Below is a basic function that takes in as input two numbers, and returns their sum as an output.
Output:
In the above-given code block, the first thing that we did was define the function itself. The def keyword denotes the beginning of the function declaration, followed by the function's name (here, add_two_nums). In the parentheses following the function name, we specified two function parameters— num1 and num2. The function adds num1 and num2, stores the result in a variable sum, and then returns the sum as the output. Thereafter, in line 5, we call the function in our code with num1 = 5 and num2 = 6 as the function arguments.
Based on the example, we can define function parameters and function arguments as follows:
Parameters are the variables that we specify inside parentheses at the time of defining a function. In the example, num1 and num2 are function parameters.
Arguments, on the other hand, are the values that are passed for these parameters when calling the function. Arguments allow us to pass information to the function. In the example above, we provided 5 and 6 as the function arguments for parameters num1 and num2, respectively.
Types of Function Arguments in Python
There are four inherent function argument types in Python, using which we can call functions to perform their desired tasks. These are as follows:
- Python Default Arguments
- Python Keyword Arguments
- Python Arbitrary Arguments
- Python Required Arguments Now let's look at different types of function arguments one by one.
Python Default Arguments
Let us look at an example where we first declare a function and then call the function later in the code. However, in this example, let us assume that we forgot to pass in the arguments at the time of the function call. The code looks something like this
Upon running, the above-given code block will throw the following error:
The error indicates that we didn’t provide an argument value for the parameters a and b. It explains the importance of providing argument values to a function and is also a common mistake that often occurs while programming. Now, the question is, how exactly can we handle this and create a failsafe method so that even if we forget to pass in the arguments, the function automatically assumes some default values in place of the missing arguments? Here comes the concept of default function arguments.
Default function arguments in python, as the name suggests, are some default argument values that we provide to the function at the time of function declaration.
Thus, when calling the function, if the programmer doesn’t provide a value for the parameter for which we specified the default argument, the function assumes the default value as the argument in that case. Thus, calling the function will not result in any error even if we don’t provide any value as an argument for the parameter with a default argument.
We declare a default argument by using the equal-to (=) operator at the time of function declaration.
Let us take a look at another example.
In the example given above, we specified the values “Elon Musk” and 57 as the default arguments for the parameters name and age, respectively, at the time of function declaration.
Then in line 4, we called the function with user-specified inputs; hence, the output printed statement consisted of the user-specified argument values.
However, in line 5, we did not provide any argument values. Hence, the function printed a statement with the default argument values.
An advantage of default function arguments in python is that it helps keep the “missing positional arguments” error in check and gives the programmer more control over their code.
Some rules that we need to keep in mind regarding default arguments are as follows:
- There can be any number of default arguments in a function.
- Whenever you declare a function, the non-default arguments are specified before the default arguments. The default arguments are specified at the end, meaning any arguments specified to the right of a default argument must also be a default one. It is to prevent any ambiguity by the Python interpreter. For example, the below-given code block will throw an error if we try to run it.
The following is the error we get after running the above-given code block:
We specified the default argument (a=0) before the non-default argument (b). The correct way to write this function will be:
Now, let us look at the next function argument type in Python.
Python Keyword Arguments
The term “keyword” is pretty self-explanatory. It can be broken down into two parts— a key and a word (i.e., a value) associated with that key.
To understand keyword arguments in Python, let us first look at the example given below.
Output:
In the above-given example, at the time of calling the function, we provided arguments 12 and 3 to the function. Thus, the function automatically assigned the argument value 12 to parameter a and 3 to parameter b, as per the order/position we specified the arguments in. Hence, 12 and 3 here are known as positional arguments.
But this brings us to an important question. What if we wanted the interpreter to assume the argument values as a=3 and b=12 in the above-given example? Is there a way we can specify argument b before a during the function call?
Here comes the concept of keyword arguments. In the case of keyword argument, the programmer manually assigns argument values to the function call.
The following example shows the use of keyword arguments in python for the above defined ‘divide_two’ function.
Output:
As you can notice, Python allows calling a function using a mixture of both positional and keyword arguments. Although in terms of the order, one must keep in mind that keyword arguments follow positional arguments. Also, a best practice is always to have only positional or keyword arguments in a function call to maintain homogeneity and code readability.
Using keyword arguments in python, the programmer can have more stricter control over the arguments that are passed to the function. It also means that the order of the arguments can be changed, which is not the case in positional arguments, where the order of arguments is according to the order of function parameters, as specified during the time of function declaration.
Another advantage of using keyword arguments over positional arguments is that it makes the code more human-readable and understandable, which is essential if you are working with a team of developers on a single code base.
With this, we have covered the keyword function arguments in Python. Let us move on to the next class of function arguments.
Python Arbitrary Arguments
Arbitrary arguments, also known as variable length arguments, play a very important role in Python. Sometimes, at the time of function declaration, the programmer might not be certain about the number of arguments to be passed to the function to run it. Another way to put it is that the number of arguments might vary each time the function is called. In these cases, we use arbitrary arguments in Python.
There are two ways to pass variable-length arguments to a python function.
The first method is by using the single-asterisk (*) symbol. The single-asterisk is used to pass a variable number of non-keyworded arguments to the function. At the time of function declaration, if we use a single-asterisk parameter (e.g.- *names), then all the non-keyword arguments passed to the function at the time of function call are collected into a single tuple before being passed to the function. We can understand this with the help of the following example.
Output:
You can also pass a variable number of keyworded arguments to a Python function. For that, we use the double-asterisk (**) operator. At the time of function declaration, using a double-asterisk parameter (eg.- **address) results in the collection of all the keyword arguments in python, passed to the function at the time of function call into a single Python dictionary before being passed to the function as input. We can understand this with the help of the following example.
Output:
The function can also have a combination of arbitrary keyword and non-keyword arguments, as shown in the example below.
Output:
In the example shown above, we can see that all the non-keyworded function arguments (i.e., “Lion”, “Elephant”, “Wolf” and “Gorilla”) were collected and stored in the tuple animals. On the other hand, all the keyworded arguments were collected in the dictionary foods.
Knowing how to use arbitrary arguments in Python lets us now understand the final category of function arguments in Python.
Python Required Arguments
Required arguments, as the name suggests, are those that are required to be passed to the function at the time of function call. Failing to do so will result in an error.
In the simplest terms, required arguments are exactly the opposite of default function arguments. As we saw earlier, in the case of default arguments, we provide a default value to the function parameters when declaring the function. While calling the function, if no argument is provided for these parameters, the function automatically assumes their default argument value. Hence, in the case of default arguments, providing a value was optional.
However, in the case of the parameters for which no default argument is provided, it is mandatory to pass an argument at the time of the function call. Otherwise, the Python interpreter throws an error indicating a missing positional argument.
The easiest way to distinguish required arguments from default arguments is that the required arguments don’t have a default value.
Let us try to understand this with the help of an example.
Output
In the example given above, line 10 will throw the following error.
It happened since we did not provide any value for the required argument num1. On the other hand, not providing a value for num2 in line 7 will not result in any error, as the function assumes the default value for num2.
Conclusion
Python is a high-level programming language that is very versatile in terms of how the programmer can code. The way the programmer leverages the different argument types in their functions depends on their need and programming style. To summarise everything that we read in this article:
- We had a brief overview of what functions are
- We understood the difference between parameters and arguments, which are often used interchangeably but are quite different
- We understood the different function argument types in Python, and how we can use them.