Functions are an integral part of every programming language. A function is a block of code that can perform a particular action. Functions make our life easy by simplifying cumbersome tasks.
In Python as well, we have a lot of in-built functions. Any statement that consists of a word followed by information in parenthesis is a function call. For example, the print statement which we use is an in-built function.
Apart from that, programming languages like Python allows creating user-defined functions. Users can define functions by using the def statement in Python.
Let’s see an example.
def shout(): print('helloooo')
In this example, we defined a function called shout. So whenever we call this function shout(), it will print “helloooo” as the output.
Functions can have arguments as well. Some people call them parameters, but there is a difference between arguments and parameters.
A parameter is a variable in a method definition or function definition. When we call a method, arguments are the real data that we pass into the parameters of the method. If you want to learn more about them, check out this article.
Let’s see an example function that uses arguments to print the sum of 2 numbers.
def add(x,y): print(x+y) add(3,6)
This function will return 9 as the output.
Types of Function Arguments in Python
There are different types of arguments available in Python, which we can use based on our needs.
The different types of arguments in Python are:
- Default Arguments
- Keyword Arguments
- Flexible Number of Arguments
So, let’s look at each type of argument in Python, along with some simple examples.
1. Default Arguments in Python
In some cases, we may want to build a function that has some default values for parameters in case if the user doesn’t provide values for them.
In such cases, we can use the default arguments. A default argument assumes a default value if a value is not supplied as an argument while calling the function.
This can be done using the assignment operator ‘=’. We can provide default values for one or more parameters.
Let’s look at an example python program.
def get_gender(sex = 'unknown'): if sex is 'm': sex = "male" elif sex is 'f': sex = "Female" print(sex) get_gender('m') get_gender('f') get_gender()
The output of the program will be:
‘ Male ‘
‘ Female ‘
‘ Unknown ‘
In this example, you can see that there is a default argument that is ‘unknown’. If the user doesn’t provide the argument in the function call, it will be used as the argument by default.
Note that any number of arguments in a function can have a default value. But, using a non-default argument after default arguments will cause a SyntaxError.
The reason for this error is because argument values are assigned to parameters in order, based on their position. The value for a non-default argument is mandatory, but the value for a default argument is optional.
2. Keyword Arguments in Python
Whenever we want to pass in a limited number of arguments or to pass in a different order, we use keyword arguments. When we use keyword arguments, the position of arguments can be changed. We can use the name of the parameter irrespective of the position in a function call.
Let’s look at a simple example program:
def mango(name='Ashwin', action='ate', item='mango'): print(name,action,item) mango() mango(item='apple') mango(name='mango',item='Ashwin')
The output of the above program will be:
‘ Ashwin ate mango ‘
Ashwin ate apple ‘
‘ mango ate Ashwin ‘
As you can see, the output changes according to the values that we give to the keyword arguments. This feature makes the use of functions easy as we don’t need to worry about the order (or position) of arguments. We can directly specify the name of the arguments irrespective of their order.
3. Flexible Number of Arguments
Sometimes, we may not know the possible number of arguments that the function can have while defining the function. If the number of arguments needed is unknown to us, then we can define the function with a flexible number of arguments.
We can create functions that take any amount of arguments using a * symbol before the keyword inside the parentheses. These are also called variable-length arguments.
Let’s see an example program that will take N numbers and find their sum.
def add(*args): total=0 for a in args: total+=a print(total) add(3,5) add(3,4,5,1,2)
The output of this program will be:
In this example, we can give as many numbers as arguments to the function. The function will consider all of them and give their sum as the output.
So, by using the flexible-number of arguments feature, we can take any number of arbitrary arguments.
We can use the * symbol to unpack a list and pass the list values in a simpler way.
Let’s see a simple example.
def add(a,b,c): sum = a+b+c my_data= [20,2,0] add(my_data,my_data,my_data)
In the above example, each value in the list called mydata is passed separately to the function. But, we can do this in a much simpler way.
This is the simpler way of calling the add function. In this case, the * operator takes the list and passes each item to the function one at a time.
In this article, we learned the three basic types of function arguments in Python programming language. These three types of arguments, namely, default arguments, keyword arguments, and variable-length arguments, are pretty useful in certain circumstances.
Now you know the concepts. You can play with these different types of arguments when you do your next Python project. You may need them in solving some problems.
If you have any doubts regarding this article or related to programming, feel free to let me know in the comments. I’ll be happy to help you.
If you want to learn more programming concepts, especially the necessary technical skills and soft skills to become a better Python programmer, check out my other articles as well.
I hope this article was helpful to you. I would appreciate it if you would be willing to share it. It will encourage me to create more useful tutorials like this.