Python Function Arguments

Video Tutorial
FREE
Parameter & Arguments thumbnail
This video belongs to
Python Course for Beginners With Certification: Mastering the Essentials
16 modules
Certificate

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 a parameter is a variable inside the parenthesis of a function. In contrast, an argument is a value passed to it.

What is a Function Argument in Python?

Below is a basic function that inputs 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. After that, 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 we specify inside parentheses when 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 Python Function Arguments

There are 4 inherent function argument types in Python, which we can call functions to perform their desired tasks. These are as follows:

  1. Python Default Arguments
  2. Python Keyword Arguments
  3. Python Arbitrary Arguments
  4. Python Required Arguments

Default Arguments

As the name suggests,Default function arguments in Python 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. 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 using the equal-to (=) operator at the time of function declaration.

Let us take a look at another example.

In the example above, we specified the values “Elon Musk” and 57 as the default arguments for the parameters' name and age 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:

Let us look at the next function argument type in 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, when 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. 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? Can we specify argument b before a during the function call?

Here comes the concept of keyword arguments. The programmer manually assigns argument values to the function call regarding the keyword argument.

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. However, 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.

The programmer can use keyword arguments in Python to have stricter control over the arguments 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 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.

Positional Arguments

Positional arguments are called according to their position in the function definition. The first argument in the call corresponds to the first parameter in the function's definition, the second to the second parameter, and so on. This approach is contrasted with keyword arguments, where each argument is assigned to a specific parameter by name, regardless of order.

  • Positional arguments make the order of parameters clear, which can be crucial in understanding the function's behaviour.
  • They provide a straightforward way to pass values to a function without remembering the parameters' names.

Let us try to understand this with the help of an example.

Output

Here, 2 and 3 are positional arguments.

Variable-length Arguments

Variable length arguments, also known as Arbitrary arguments, play an essential role in Python. Sometimes, at the time of function declaration, the programmer might need to be more specific 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.

1. Arbitrary Positional Arguments (*args)

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), all the non-keyword arguments passed during the 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:

2. Arbitrary Keyword Arguments (**kwargs)

You can also pass several 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 (e.g., **address) results in collecting 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.

Important Points to Remember about Function Argument

1. Necessity of Positional Arguments Before Default Arguments

In Python, arguments with default values (default arguments) must be defined after all the positional arguments without default values. This ensures the interpreter correctly assigns values to arguments when the function is called. For example:

Output:

2. Positional Arguments Precede Keyword Arguments

When calling a function, arguments without a keyword (positional arguments) should come before those specified with keywords. This rule helps the Python interpreter understand which values correspond to which parameters. For example:

Output:

3. Flexibility in Ordering Keyword Arguments

While the sequence of keyword arguments in a function call doesn't matter, each keyword argument must correspond to a parameter defined in the function. This allows more readability and flexibility in function calls. For example:

Output:

4. Unique Assignment for Each Argument

When calling a function, ensure that no argument receives a value more than once. Python will raise an error if a parameter is given multiple values in a function call. For example:

Output:

Conclusion

Python is a high-level programming language versatile in how the programmer can code. How 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 Python's different function argument types and how to use them.

Learn More