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.
In this example, the function
2 is defined with three parameters,
5, and print the sum of all three values of the arguments passed during a function call.
Total is: 120
function argument and parameter
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
6 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.
Types of Python function arguments explained with examples
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.
Let’s define a function
9 with four arguments
3. In this function,
3 are default arguments with default values.
Passing one of the default arguments:
If you pass values of the
3 arguments while calling a function, then those values are used instead of default values.
Student Details: Kelly 12 Six ABC School Student Details: Jessa 12 Seven XYZ School
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 Details: Kelly 12 Six ABC School Student Details: Jessa 12 Seven XYZ School4, the value “Jon” gets assigned to the argument name, and similarly, 12 to
1 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 (
Student Details: Kelly 12 Six ABC School Student Details: Jessa 12 Seven XYZ School6) assignment operator. The Keyword Argument is also called a named argument.
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.
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.
Total is: 1200
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
Total is: 1201
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.
Total is: 1202
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.
Total is: 1203
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.
Total is: 1204
Point 4: No argument should receive a value more than once
Total is: 1205
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:
The *args and **kwargs allow you to pass multiple positional arguments or keyword arguments to a function.
Arbitrary positional arguments (# function with 2 keyword arguments grade and school def student(name, age, grade="Five", school="ABC School"): print('Student Details:', name, age, grade, school) # without passing grade and school # Passing only the mandatory arguments student('Jon', 12) # Output: Student Details: Jon 12 Five ABC School7)
We can declare a variable-length argument with the
0 (asterisk) symbol. Place an asterisk (
0) 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:
Total is: 1206
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.
Total is: 1207
Total is: 1208
Note: args is just a name. You can choose any name that you prefer, such as *subjects.
Total is: 1209
Arbitrary keyword arguments (**kwargs)
We saw how to use
7. Now let’s see how to use the
8 argument. The **kwargs allow you to pass multiple keyword arguments to a function. Use the
8 if you want to handle named arguments in a function.
Use the unpacking operator(
5) 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).
Can you change the value of a parameter in a function?
By-Value: The value of a variable is sent to function. The actual parameter cannot be changed by function. By-Reference An address gets sent to function.
Why can we not modify parameter variables in a function?
The problem here is that if the input parameters are modified at some point in the routine, they will no longer represent their original values. Then, developers who come later may be operating under some false assumptions. This then leads to severe bugs in logic, that aren't easily caught.
Can a parameter be changed?
If you want to change a parameter, you have have its address, which means you have to pass it by reference. In general, value parameters are used for input, and reference parameters are used for input/output (maybe just output).
When can functions modify their arguments?
Some functions work by modifying the values of their arguments. This may be done to pass more than one value back to the calling routine, or because the return value is already being used in some way.