A function in Python is a block of reusable code that will be executed when it is called. We can call a function any number of times. Arguments are a type of information or value that are specified within parentheses and can be passed to a function.
Functions can have zero, one, or more arguments. Arguments are optional in Python.
There are four types of function arguments:
Required arguments must be passed in the correct positional order. The number of required arguments in the function call and function definition must be the same.
def printing(name, age):print("My name is:", name)print("My age is:", age)# main codeprinting("neha", 20)# arguments will be passed to the function based on the orderprinting(20, "neha")
If we pass more or less required arguments than those in the function definition, we get an error. Required arguments must be given before the default, keyword, and variable-length arguments.
def printing(name, age):print("My name is:", name)print("My age is:", age)# main codeprinting("neha", 20, 10)# the code below also gives an error if we pass less no of argumentsprinting("neha")
Default arguments are those arguments that are only initialized in the function definition. These arguments are optional in the function call. If any value is passed in the functional call, that value is taken; otherwise, the value initialized during the function call is taken.
def printing(name, age = 10):print("My name is:", name)print("My age is:", age)# main codeprint("When default argument not passed age = 10 is taken")printing("neha")print("When default argument is passed age = 29 is taken")printing("neha", 29)
def printing(name = "neha", age = 10):print("My name is:", name)print("My age is:", age)# function callprinting()printing("riya")
In the function definition, required arguments must be defined before the default arguments; otherwise, we will get an error.
# non-default argument follows default argument in the headerdef printing(name, age = 10, address):print("My name is:", name)print("My age is:", age)print("My address is:", address)# function callprinting("riya", 20, "hyderabad")
Keyword arguments are related to the function calls. We can give keyword arguments in any order as they will be identified by the parameter names and be passed according to the Python interpreter’s function.
def printing(name, age = 10):print("My name is:", name)print("My age is:", age)# function callprinting(age = 29, name = "neha")printing(name = "neha")
Keyword arguments and default arguments must be passed after required arguments. In the example below, name
is the required argument, age
is the default argument, and address="chandanagar"
is a keyword argument. Since “neha” is passed after age and address, we get an error.
def printing(name, address, age = 10):print("My name is :", name)print("My age is:", age)print("My address is:", address)# function callprinting(age = 29, address = "chandanagar", "neha")
In some projects, we may not know the number of arguments to be passed while coding. In such a case, we can use these variable-length arguments. We can pass any number of arguments according to our requirements.
We have two kinds of variable-length arguments:
a) *args
b) **kwargs
*args
is a tuple datatype argument that holds the values of all non-keyword variable arguments. The values passed to this argument are optional.
def printing(*names):for i in names:print(i)print(type(names))# function callprint("passing 3 arguments")printing("neha", "riya", "ravi")print("passing 5 arguments")printing("neha", "riya", "ravi", "surya", "kaushika")
def printing(age, address = "Hyderabad", *names):print("age = ", age)print("address = ", address)for i in names:print(i)print(type(names))#function callprinting(20, "chandanagar", "neha", "riya", "ravi")print("****another example****")printing(20, "chandanagar", "neha", "riya", "ravi")
**kwargs
is a dictionary datatype argument that holds the values of all key-value pair arguments. Values passed to this argument are optional.
def printing(age, address, *names, **kwargs):print("age = ", age)print("address = ", address)print("names = ", names)print("kwargs = ", kwargs)#function callprinting(20, "chandanagar")
def printing(age, address, *names, **kwargs):print("age = ", age)print("address = ", address)print("names = ", names)print("kwargs = ", kwargs)#function callprinting(20, "chandanagar", "neha", "riya", "ravi", fruit = "orange", Number = 78, Vegetable = "brinjal")
def printing(age, address = "Hyderabad", *names, **kwargs):print("age = ", age)print("address = ", address)print("names = ", names)print("kwargs = ", kwargs)#main codeprinting(20, "neha", "riya", "ravi", fruit = "orange", Number = 78, Vegetable = "brinjal")
The order and number of required arguments in a function call must match the order and number of required arguments in the function definition.
Default arguments must be given after the required arguments.
Keyword arguments must be given after the required arguments in the function call.
Default arguments may or may not be given in the function call.
Keyword arguments can be given in any order in the function call.
*args is of tuple datatype.
**kwargs is of dictionary datatype.