In this tutorial, we will discuss variable function arguments. In the previous tutorials of Python function and Python user defined functions we learned that we call the function with fixed number of arguments, for example if we have defined a function to accept two arguments, we have to pass the two arguments while calling the function. Here we will see how to call the function with variable arguments using Default, Keyword and Arbitrary arguments.
Table of Contents
Before we discuss variable function arguments let’s see the errors we get when we call the function with variable arguments without using Default, Keyword and Arbitrary arguments.
Here we have defined a function
demo() with two parameters
age, which means this function can accept two arguments while we are calling it.
def demo(name,age): """This function displays the age of a person""" print(name + " is " + age + " years old") # calling the function demo("Mohan","20")
Let’s see what happens we call this function with variable number of arguments:
Here we are calling the function demo() with a single argument, as you can see in the output that we got an error.
def demo(name,age): print(name + " is " + age + " years old") # calling the function demo("Mohan")
TypeError: demo() missing 1 required positional argument: 'age'
Here we are calling the function with no arguments and we are getting an error.
def demo(name,age): print(name + " is " + age + " years old") # calling the function demo()
TypeError: demo() missing 2 required positional arguments: 'name' and 'age'
Till now we have seen the fixed function arguments in Python. We have also learned that when we try to pass the variable arguments while calling a function, we get an error.
Now it’s time to learn how to pass variable arguments in function using Default, Keyword and Arbitrary arguments.
We can provide default values to the function arguments. Once we provide a default value to a function argument, the argument becomes optional during the function call, which means it is not mandatory to pass that argument during function call. Let’s take an example to understand this concept.
Here in this example, we have provided the default value to the parameter
age using the assignment (=) operator.
In the following example parameter
name doesn’t have a default value, so it is mandatory during function call. On the other hand, the parameter
age has a default value, so it is optional during a function call.
1. If you provide a value to the default argument during a function call, it overrides the default value. For example, in the following program we have overriden the age of Lucy & Bucky to 20 & 40 respectively.
2. A function can have any number of default arguments, however if an argument is set to be a default, all the arguments to its right must always be default. For example, def demo(name = “Mohan”, age): would throw an error (SyntaxError: non-default argument follows default argument) because name is a default argument so all the arguments that are following it, must always be default.
def demo(name, age = "30"): """ This function displays the name and age of a person If age is not provided, it's default value 30 would be displayed. """ print(name + " is " + age + " years old") demo("Steve") demo("Lucy", "20") demo("Bucky", "40")
We have learned that when we pass the values during function call, they are assigned to the respective arguments according to their position. For example if a function is defined like this:
def demo(name, age): and we are calling the function like this:
demo("Steve", "35") then the value “Steve” is assigned to the argument
name and the value “35” is assigned to the argument
age. Such arguments are called positional arguments.
Python allows us to pass the arguments in non-positional manner using keyword arguments. Lets take an example to understand this:
def demo(name, age): print(name + " is " + age + " years old") # 2 keyword arguments (In order) demo(name = "Steve", age = "35") # 2 keyword arguments (Not in order) demo(age = "20", name = "Mohan") # 1 positional and 1 keyword argument demo("Bucky", age = "40")
Note: In the above example, during third function call demo(“Bucky”, age = “40”) we have keyword argument and non-keyword argument. In the example keyword argument follows (is after) the non-keyword argument. However if you try to place the keyword argument before the non-keyword argument, it would throw the following error:
SyntaxError: non-keyword arg after keyword arg
So please keep this in mind that when you are mixing the positional and keyword arguments, the keyword argument must always be after the non-keyword argument (positional argument).
Python allows us to have the arbitrary number of arguments. This is especially useful when we are not sure in the advance that how many arguments, the function would require.
We define the arbitrary arguments while defining a function using the asterisk (*) sign.
def fruits(*fnames): """This function displays the fruit names""" # fnames is a tuple with arguments for fruit in fnames: print(fruit) fruits("Orange","Banana","Apple","Grapes")
In the above example we are using the * before the parameter
fnames that allows us to accept the arbitrary number of arguments during function call.