In Python, function that takes variable number of arguments. Information can be passed into functions as arguments.
Arguments are specified after the function name, inside the parentheses. An argument is the value that are sent to the function when it is called. We can add as many arguments as we want, just separate them with a comma.
There are other ways to define a function that can take variable number of arguments, three different forms of this type are described below:
Default Argument in Python
Function arguments can have default values in Python. We can assign a default value to an argument using the assignment operator in python(=).
When we call a function without a value for an argument, its default value is used.
def name(name,msg="Good Morning "): print("Hello",name+','+msg) name("abc") name("xyz","How are you?")
Hello abc,Good Morning Hello xyz,How are you?
In this example, the parameter
name does not have a default value and is required during a call.
On the other hand, the parameter
msg has a default value of
"Good Morning". So, it is optional during a call. If a value is provided, it will overwrite the default value.
Any number of arguments in a function can have a default value. But once we have a default argument, all the arguments to its right must also have default values.
Python Keyword Arguments
When we call a function with some values, these values get assigned to the arguments according to their position.
With keyword arguments in python, we can change the order of passing the arguments without any consequences.
def add(a, b): return a + b print(add(a=3, b=4))
We can call this function with arguments in any order, as long as we specify which value goes into what.
add(b = 3,a = 4)
These are keyword python function arguments.
Python Arbitrary Arguments
In Python, sometimes we do not know in advance the number of arguments that will be passed into a function. It allows us to handle this kind of situation through function calls with an arbitrary number of arguments.
In the function definition, we use an asterisk (*) before the parameter name to denote this kind of argument.
def func_1(*names): for name in names: print("Good Morning !!",name) func_1("A", "B", "C")
Good Morning !! A Good Morning !! B Good Morning !! C
This was all about the Python Function Arguments.