This article explains Python’s various function arguments with clear examples of how to use them. But before learning all function arguments in detail, first, understand the use of argument or parameter in the function.
Table of contents
What is a function argument?
When we define and call a Python function, the term parameter and argument is used to pass information to the function.
- parameter: It is the variable listed inside the parentheses in the function definition.
- argument: It is a value sent to the function when it is called. It is data on which function performs some action and returns the result.
Example:
In this example, the function sum_marks()
is defined with three parameters, a
, b
, c
, and print the sum of all three values of the arguments passed during a function call.
Output:
Total is: 120
Note: A function must be called with the correct number of arguments by default. For example, the above function expects 3 arguments, so you have to call the my_sum()
function with 3 arguments; otherwise, you will get an error.
Does function need arguments?
It is not mandatory to use arguments in function definition. But if you need to process user data, you need arguments in the function definition to accept that data.
Also, we use argument in function definition when we need to perform the same task multiple times with different data.
Can a function be called without arguments?
If the function is defined with parameters, the arguments passed must match one of the arguments the function accepts when calling.
Types of function arguments
There are various ways to use arguments in a function. In Python, we have the following 4 types of function arguments.
- Default argument
- Keyword arguments (named arguments)
- Positional arguments
- Arbitrary arguments (variable-length arguments
*args
and**kwargs
)
Default Arguments
In a function, arguments can have default values. We assign default values to the argument using the ‘=’ (assignment) operator at the time of function definition. You can define a function with any number of default arguments.
The default value of an argument will be used inside a function if we do not pass a value to that argument at the time of the function call. Due to this, the default arguments become optional during the function call.
It overrides the default value if we provide a value to the default arguments during function calls.
Let us understand this with an example.
Example:
Let’s define a function student()
with four arguments name
, age
, grade
, and school
. In this function, grade
and school
are default arguments with default values.
- If you call a function without
school
andgrade
arguments, then the default values ofgrade
andschool
are used. - The
age
andname
parameters do not have default values and are required (mandatory) during a function call.
Passing one of the default arguments:
If you pass values of the grade
and school
arguments while calling a function, then those values are used instead of default values.
Example:
Output:
Student Details: Kelly 12 Six ABC School Student Details: Jessa 12 Seven XYZ School
Keyword Arguments
Usually, at the time of the function call, values get assigned to the arguments according to their position. So we must pass values in the same sequence defined in a function definition.
For example, when we call student('Jon', 12, 'Five', 'ABC School')
, the value “Jon” gets assigned to the argument name, and similarly, 12 to age
and so on as per the sequence.
We can alter this behavior using a keyword argument.
Keyword arguments are those arguments where values get assigned to the arguments by their keyword (name) when the function is called. It is preceded by the variable name and an (=
) assignment operator. The Keyword Argument is also called a named argument.
Example:
Output:
Student Details: Jessa 14 Student Details: Jon 12 Student Details: Donald 13
Change the sequence of keyword arguments
Also, you can change the sequence of keyword arguments by using their name in function calls.
Python allows functions to be called using keyword arguments. But all the keyword arguments should match the parameters in the function definition. When we call functions in this way, the order (position) of the arguments can be changed.
Example:
# both keyword arguments by changing their order
student(age=13, name='Kelly')
# Output: Student Details: Kelly 13
Code language: Python (python)
Positional Arguments
Positional arguments are those arguments where values get assigned to the arguments by their position when the function is called. For example, the 1st positional argument must be 1st when the function is called. The 2nd positional argument needs to be 2nd when the function is called, etc.
By default, Python functions are called using the positional arguments.
Example: Program to subtract 2 numbers using positional arguments.
Note: If you try to pass more arguments, you will get an error.
Output
TypeError: add() takes 2 positional arguments but 3 were given
Note: In the positional argument number and position of arguments must be matched. If we change the order, then the result may change. Also, If we change the number of arguments, we will get an error.
Important points to remember about function argument
Point 1: Default arguments should follow non-default arguments
Example:
Point: Default arguments must follow the default argument in a function definition
Default arguments must follow the default argument. For example, When you use the default argument in a definition, all the arguments to their right must also have default values. Otherwise, you’ll get an error.
Example:
Point 2: keyword arguments should follow positional arguments only.
we can mix positional arguments with keyword arguments during a function call. But, a keyword argument must always be after a non-keyword argument (positional argument). Else, you’ll get an error.
I.e., avoid using keyword argument before positional argument.
Example:
Point 3: The order of keyword arguments is not important, but All the keyword arguments passed must match one of the arguments accepted by the function.
Example:
Point 4: No argument should receive a value more than once
Variable-length arguments
In Python, sometimes, there is a situation where we need to pass multiple arguments to the function. Such types of arguments are called arbitrary arguments or variable-length arguments.
We use variable-length arguments if we don’t know the number of arguments needed for the function in advance.
Types of Arbitrary Arguments:
- arbitrary positional arguments (
*args
) - arbitrary keyword arguments (
**kwargs
)
The *args and **kwargs allow you to pass multiple positional arguments or keyword arguments to a function.
Arbitrary positional arguments (*args
)
We can declare a variable-length argument with the *
(asterisk) symbol. Place an asterisk (*
) before a parameter in the function definition to define an arbitrary positional argument.
we can pass multiple arguments to the function. Internally all these values are represented in the form of a tuple. Let’s understand the use of variable-length arguments with an example.
This is a simple function that takes three arguments and returns their average:
This function works, but it’s limited to only three arguments. What if you need to calculate the average marks of more than three subjects or the number of subjects is determined only at runtime? In such cases, it is advisable to use the variable-length of positional arguments to write a function that could calculate the average of all subjects no matter how many there are.
Example:
Output:
Average = 63.33
Note: args is just a name. You can choose any name that you prefer, such as *subjects.
Arbitrary keyword arguments (**kwargs)
We saw how to use *args
. Now let’s see how to use the **kwargs
argument. The **kwargs allow you to pass multiple keyword arguments to a function. Use the **kwargs
if you want to handle named arguments in a function.
Use the unpacking operator(**
) to define variable-length keyword arguments. Keyword arguments passed to a kwargs are accessed using key-value pair (same as accessing a dictionary in Python).
Example:
Output:
math = 56 english = 61 science = 73