Formal and Actual Arguments
When a function is defined, it may have some parameters. These parameters are useful to receive values from outside of the function. They are called 'formal arguments'. When we call the function, we should pass data or values to the function. These values are called 'actual arguments'. In the following code, 'a' and 'b' are formal arguments and 'x' and 'y' are actual arguments.
def sum(a, b): #a, b are formal arguments
c = a+b
print(c)
x=10
y=15
sum(x, y)
The actual arguments used in a function call are of 4 types:
- Positional arguments
- Keyword arguments
- Default arguments
- Variable length arguments
1) Positional Arguments
These are the arguments passed to a function in correct positional order. Here, the number of arguments and their positions in the function definition should match exactly with the number and position of the argument in the function call. For example, take a function definition with two arguments as:
def attach(s1, s2)
attach('New', 'York')
Also, if we try to pass more than or less than 2 strings, there will be an error. For example, if we call the function by passing 3 strings as:
attach('New', 'York', 'City') #results error
A Python program to understand the positional arguments of a function.
def attach(s1, s2):
s3 = s1+s2
print('Total string: '+s3)
attach('New', 'York')
2) Keyword Arguments
Keyword arguments are arguments that identify the parameters by their names. For example, the definition of a function that displays grocery item and its price can be written as:
def grocery(item, price):
At the time of calling this function, we have to pass two values and we can mention which value is for what. For example,
grocery(item='Sugar', price=50.75)
Here, we are mentioning a keyword 'item' and its value and then another keyword 'price' and its value. Please observe these keywords are nothing but the parameter names which receive these values. We can change the order of the arguments as:
grocery(price=88.00, item='Oil')
In this way, even though we change the order of the arguments, there will not be any problem as the parameter names will guide where to store that value.
Python program to understand the keyword arguments of a function.
def grocery(item, price):
print('Item = %s'% item)
print('Price = %.2f'% price)
grocery(item='Sugar', price=50.75)
grocery(price=88.00, item='Oil')
Default Arguments
We can mention some default value for the function parameters in the definition. Let's take the definition of grocery() function as:
def grocery(item, price=40.00):
Here, the first argument is 'item' whose default value is not mentioned. But the second argument is 'price' and its default value is mentioned to be 40.00.
At the time of calling this function, if we do not pass 'price' value, then the default value of 40.00 is taken. If we mention the 'price' value, then that mentioned value is utilized. So, a default argument is an argument that assumes a default value if a value is not provided in the function call for that argument.
A Python program to understand the use of default arguments in a function.
def grocery(item, price=40.00):
print('Item = %s'% item)
print('Price = %.2f'% price)
grocery(item='Sugar', price=50.75)
grocery(item='Sugar')
Variable Length Arguments
Sometimes, the programmer does not know how many values a function may receive. In that case, the programmer cannot decide how many arguments to be given in the function definition. For example, if the programmer is writing a function to add two numbers, he can write:
add(a, b)
But, the user who is using this function may want to use this function to find sum of three numbers. In that case, there is a chance that the user may provide 3 arguments to this function as:
add(10, 15, 20)
Then the add() function will fail and error will be displayed.If the programmer wants to develop a function that can accept 'n' arguments, that is also possible in Python. For this purpose, a variable length argument is used in the function definition. A variable length argument is an argument that can accept any number of values.
The variable length argument is written with a' * ' symbol before it in the function definition as:
def add(farg, *args):
Here, 'farg' is the formal argument and '*args' represents variable length argument. We can pass 1 or more values to this '*args' and it will store them all in a tuple.
A Python program to show variable length argument and its use. #variable length argument demo
def add(farg, *args):
print('Formal argument=', farg)
sum=0
for i in args:
sum+=i
print('Sum of all numbers= ',(farg+sum))
add(5, 10)
add(5, 10, 20, 30)
A keyword variable length argument is an argument that can accept any number of values provided in the format of keys and values. If we want to use a keyword variable length argument, we can declare it with ' ** ' before the argument as:
def display(farg, **kwargs):
Here '**kwargs' is called keyword variable argument. This argument internally represents a dictionary object. A dictionary stores data in the form of key and value pairs. It means, when we provide values for '**kwargs', we can pass multiple pairs of values using keywords as:
display(5, rno=10)
Here, 5 is stored into 'farg' which is formal argument. 'rno' is stored as key and its value '10' is stored as value in the dictionary that is referenced by 'kwargs'.
A Python program to understand keyword variable argument.
def display(farg, **kwargs):
print('Formal argument=', farg)
for x, y in kwargs.items(): #items() will give pairs of items
print('key = {}, value = {}'.format(x, y))
#pass 1 formal argument and 2 keyword arguments
display(5, rno=10)
print()
#pass 1 formal argument and 4 keyword arguments
display(5, rno=10, name='Prakash')