In the article Python Functions, we learned how to define the user-defined functions and use them. Let’s see an example of a python function.
Code:
Python Code
def wish(name):
print("Hello " + name + ", " + "welcome to TakeUforwrad")
wish("Raj")
Output: Hello Raj, welcome to TakeUforwrad
In the above code, wish() is a function with one argument. This function welcomes the person to the site. Here we passed a single argument, so the function worked smoothly. Let’s try to pass more and fewer arguments than the function required, and see what happens.
Function with more arguments than required
Code:
Python Code
def wish(name):
print("Hello " + name + ", " + "welcome to TakeUforwrad")
wish("Raj", "vikramaditya")
Output: TypeError: wish() takes 1 positional argument but 2 were given
Function with fewer arguments than required
Code:
Python Code
def wish(name):
print("Hello " + name + ", " + "welcome to TakeUforwrad")
wish()
Output :
TypeError: wish() missing 1 required positional argument: ‘name’
We can see that passing more or fewer arguments than required, raises errors.
Variable Function Arguments :
So far we have seen the function with fixed arguments, python also supports the functions of variable arguments. There are 3 ways to define the functions which take a variable number of arguments.
The three ways are :
- Python Default Arguments
- Python Keyword Arguments
- Python Arbitrary Arguments
Let’s look at them one by one.
Python Default Arguments
Python functions can have default arguments. That means we can provide the default values to the arguments using the assignment operator ( = ). If any value is not passed in the function call for the default argument, the argument takes the default value. So In this way, we can define the variable number of arguments. Let’s look at an example
Code:
Python Code
# example for python default arguments
def wish(name, msg="Welcome to TakeUforward"):
print("Hello " + name + ", " + msg)
wish("Raj") # function call with fewer arguments
wish("Raj", "Good Morning")
Output :
Hello Raj, Welcome to TakeUforward
Hello Raj, Good Morning
In the function wish() we have provided the default value to the argument ‘ msg ‘. In the first function call, we passed only one parameter. As we did not provide any value to the default argument the default value is taken.
In the second function call, we provided the value to the default argument. The provided value will overwrite the default value. Hence the provided value is taken for the default argument.
NOTE: In function, any number of arguments can have a default value, but all the further arguments after a default argument must and should be the default arguments.
That means non-default arguments should not follow the default arguments.
Code:
Python Code
# example for python default arguments
def wish(msg="Welcome to TakeUforward",name):
print("Hello " + name + ", " + msg)
wish("Raj")
Output :
SyntaxError: non-default argument follows default argument
In the wish() function non-default argument ‘ name ‘ is followed by the default argument ‘ msg ‘
So the error is raised.
Python Keyword Arguments :
The values passed in the function call are assigned to the arguments according to their position. If the order is changed the arguments take different values than expected. Let’s look at an example
Code :
Python Code
def wish(msg, name):
print('value of msg is ', msg)
print('value of name is ', name)
wish("Raj", "Welcome to TakeUforward")
Output :
value of msg is Raj
value of name is Welcome to TakeUforward
According to their position, the value ‘ Raj ’ is stored in argument ‘ msg ‘, and ‘ Welcome to TakeUforward ’ is stored in argument ‘ name ‘, which is not desired. So We should be careful about the order of the parameters in the function call.
To avoid the order issue, python allows functions to be called using keyword arguments. When the function is called using keyword arguments the order does not matter. The corresponding keyword gets the provided value.
Code :
Python Code
def wish(msg, name):
print('value of msg is ', msg)
print('value of name is ', name, '\n')
wish("Raj", "Welcome to TakeUforward")
wish(name="Raj", msg="Welcome to TakeUforward") # keyword arguments
Output :
value of msg is Raj
value of name is Welcome to TakeUforward
value of msg is Welcome to TakeUforward
value of name is Raj
In the second function call even though the order is changed, the values get assigned to the arguments correctly. This is how we can use the keyword arguments.
NOTE: All the arguments after a keyword argument should be keyword arguments. That means keyword arguments should not follow by positional arguments.
Code:
Python Code
def wish(name,msg):
print('value of msg is ', msg)
print('value of name is ', name, '\n')
wish(name="Raj", "Welcome to TakeUforward")
Output :
SyntaxError: positional argument follows keyword argument
In the above function called keyword argument, ‘ name ‘ is followed by a positional argument. So the error is raised.
Python Arbitrary Arguments
We encounter situations where we don’t know the number of arguments to be passed in advance. Through function calls with an arbitrary number of arguments we can handle this situation in python.
To make an arbitrary argument use an asterisk ( * ) before the parameters in the function definition. This tells the python, that arbitrary arguments are going to be passed.
All the arguments passed will get wrapped into a tuple. We can access these arguments through indexing or using for loop.
Code :
Python Code
def team(*teammembers):
for member in teammembers:
print('Welcome to team ', member)
team('raj', 'saisri', 'Abhishek', 'sai')
Output :
Welcome to team raj
Welcome to team saisri
Welcome to team Abhishek
Welcome to team sai
Special thanks to SaiSri Angajala for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article. If you want to suggest any improvement/correction in this article please mail us at [email protected]