In this tutorial, I am going to discuss about Arguments and Parameters used for functions in python. We will also discuss about types of arguments and how to use them together.
Arguments and Parameters
We have discussed about arguments and parameters for functions while discussing about functions in python.
-
Arguments are the values that are passed to function in the function call. These are also known as actual arguments or actual parameters.
-
Parameters are the values that are received by the function in function header. These are also known as formal parameters or formal arguments.
We have seen while discussing about functions in python, number of actual arguments are equal to formal arguments. This means, if we have three arguments in the function header then there should be three parameters in the function call.
There are mainly two types of formal arguments in python :
- Positional or Required Arguments
- Variable Arguments
Let's discuss about these one by one :
Positional or Required Arguments
Positional Arguments are the simplest type of formal arguments. For these type of arguments we have to match the number of actual arguments and formal arguments.
Let's see an example :
def verify_me(p, q, r):
''' This function takes three arguments and prints them in a statement. '''
print("\nInside function : ")
print("p = ",p,", q = ",q, ", r = ",r)
a, b, c = 2, 4, 6
#all arguments are variables
verify_me(a, b, c)
#variables, expressions and literals as arguments
verify_me(a, 8, b+5)
#all arguments are literals
verify_me(3,5,7)
verify_me(2,6)
Inside function :
p = 2 , q = 4 , r = 6
Inside function :
p = 2 , q = 8 , r = 9
Inside function :
p = 3 , q = 5 , r = 7
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
< ipython-input-9-0e95df8be42c > in < module >
8 verify_me(3,5,7) #all arguments are literals
9
---> 10 verify_me(2,6)
TypeError: verify_me() missing 1 required positional argument: 'r'
Observe that in the above example, we can pass literals, variables or expressions as formal arguments. But these need to be the same number of actual arguments and formal arguments.
Also we can see that even if we omit one argument then we get TypeError and shows the message that function call is missing 1 required positional argument.
Variable Arguments
Until now, we have seen fixed number of arguments but in python we can also define functions with variable number of arguments. These type of arguments means we can give variable number of arguments to the function.
There are three different forms of variable arguments as shown below :
- Default Arguments
- Keyword Arguments
- Arbitrary Arguments
Let's discuss about these types in detail :
Default Arguments
Default Arguments in python means that arguments can have default values. So, it is possible to omit such arguments from function call and the function will take default value for such arguments.
A default argument is given in the function header by equating an actual argument to a value or literal which we want to be its default value.
Let's see some examples :
def wish_me(name = 'XYZ', wish = 'Good morning') :
''' This function takes two arguments : name and wish, and prints out a greeting statement using them '''
print("Hello {}, {}\n".format(name, wish))
wish_me()
wish_me('Kristen')
wish_me('Paul','Have a nice day')
Hello XYZ, Good morning
Hello Kristen, Good morning
Hello Paul, Have a nice day
As you can observe in the above example, when we give default value to parameters in the function header then we can omit such arguments and also can change the default value by providing actual parameters in the function call.
Note that the values for the arguments get assigned or omitted on the basis of their position in the function header and function call.
But what if we want to omit value after the first position, it is not possible using default arguments but for such cases we can use keyword arguments.
Keyword Arguments
Keyword arguments in python are used when we do not want the order or position of the arguments to interfere. Using these type of arguments we can change the order of arguments in the function call and also give value for the arguments that we want.
Let's see some examples :
wish_me(name = 'John', wish = 'How are you ?')
wish_me(wish = 'Good evening', name = 'Anne')
wish_me(wish = 'Have a nice day')
Hello John, How are you ?
Hello Anne, Good evening
Hello XYZ, Have a nice day
As we can see from the above example, using keyword arguments we can change the position of arguments and also omit any default argument we want.
Arbitrary Arguments
Arbitrary arguments are used when do not know the exact number of arguments we will be passing to the function. In such case we can use Arbitrary arguments for the function which will let us pass any number of arguments we want.
For using Arbitrary arguments, the function header should have an asterisk ( * ) before the parameter name to denote arbitrary argument.
Let's see some examples :
def values(*var) :
'''This function takes multiple values and prints them out one by one.'''
for v in var:
print(v)
print("var : ",var,", type : ",type(var))
values(1,2,3,4,5)
1
2
3
4
5
var : (1, 2, 3, 4, 5) , type : < class 'tuple' >
So, we can see that the function treats arbitrary argument as a tuple object. And we can manipulate it just like tuples.
Using Multiple Types of arguments
In python, we can use multiple types of arguments in a function call. But there are certain conditions which we need to follow while using multiple types of arguments together in a function call. The conditions are shown below :
-
First of all we need to make sure that positional or required arguments should be first in the argument list and then we can add keyword or default arguments. Because if we put default or keyword arguments before positional arguments will result in a SyntaxError.
-
We cannot specify value for an argument more than once in the function call, if we use keyword or default arguments then make sure we do not specify any value unintentionally for the same position again.
-
It is preferred to keep Arbitrary arguments at last in the function call, otherwise arguments after that will not receive any value from the function call.
Let's have a look at some examples to understand better :
def values( a, b, c = 7, d = 3 ):
print("a = ",a,", b = ",b,", c = ",c,", d = ",d)
# Positional Arguments given as keyword arguments
values(a = 9, b = 8)
#Keyword arguments can be given in any order
values(c = 6, a = 0, b = 32)
#Keyword argument after required positional arguments and omitted argument has default value
values('a', 'b', d = 64)
#Multiple values for same argument : a
values('a1', 32, a = 'a2')
a = 9 , b = 8 , c = 7 , d = 3
a = 0 , b = 32 , c = 6 , d = 3
a = a , b = b , c = 7 , d = 64
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
< ipython-input-29-6cd4ae347778 > in < module >
5 values(c = 6, a = 0, b = 32) #Keyword arguments can be given in any order
6 values('a', 'b', d = 64) #Keyword argument after required positional arguments and omitted argument has default value
----> 7 values('a1', 32, a = 'a2') #Multiple values for same argument : a
TypeError: values() got multiple values for argument 'a'
In the last function call, we have given multiple values to assign to the same argument a. One value using the position and another using it as keyword argument, that is why it throws TypeError with appropriate message.
values(1, c = 'c', d = 'd')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
< ipython-input-30-4487490b2e30 > in < module >
----> 1 values(1, c = 'c', d = 'd')
TypeError: values() missing 1 required positional argument: 'b'
As we know, positional or required arguments should always have a value in the function call either at its position in the header or by specifying it as a keyword argument.
In the above example we are omitting argument b from the function call and that is why it throws TypeError which shows required positional argument missing.
values(d = 'd1', c = 'c1', 3, 5)
File "< ipython-input-31-86c6107499e5 >", line 1
values(d = 'd1', c = 'c1', 3, 5)
^
SyntaxError: positional argument follows keyword argument
In the above function call, we can observe that we have used keyword arguments before required positional argument and that is why we get a SyntaxError.
Let's conclude this tutorial here. In this tutorial we have discussed about arguments and parameters of a function, types of arguments and how to use them together.
If you have any questions please comment below and also share your views and suggestions in the comment box.