Python Function Arguments
We now know the fact, that we can pass arguments to the function. Now, we are going to address some different types of arguments here. You might be familiar with some of those types already, and there would be a few, which we have not used yet, and are going to explore now. So, here are the different types of arguments –
Python Function Arguments
- Positional arguments.
- Default arguments.
- Arbitrary arguments.
- Keyword arguments.
- Arbitrary keyword arguments.
Positional arguments in Python
When we create some function, if we are going to provide some data to the function, we list out some parameters in the function definition. There is a certain order in which the parameters are given there. If we are giving the arguments in the same order, then the arguments are going to be said as positional arguments. We can simply say that we have been making use of positional arguments for a while now. Let’s have a look at a simple example, which gives us an idea about the positional arguments –
As you can see, we have created a function, with some parameters here, and those parameters are in a certain order. Here, when we are providing the arguments, we are providing them in the same order as the parameters. So, in this case, these are the positional arguments.
You can also perform some similar programs so that you can get familiar with the positional arguments.
Default arguments in Python
When we are writing some functions, we list out some parameters as well. Now, when it comes to providing arguments to the function, we need to provide all the positional arguments. If we miss out on some argument, we get an error. Have a look at the below program, to understand the same thing –
Now here in the above program, we are missing one argument while we are calling the function. Due to this, we get an error, saying that one positional argument is missing. You can try to write and execute the above program and observe the error.
The error came because we did not give all the necessary arguments to the function at the time of calling. But here, we can have an escape. We can do something like if we are not providing some particular argument, then it should get the default value. For example, in the above program, we are not providing any message, so there should be a default message in this case, which would be used.
So, in such situations, we can have default arguments, which simply means that the arguments can have some default values. In order to give some default value, we have to use the assignment operator and give the value in the function definition. Have a look at the below example, which gives us the same idea –
As you can see, we have given the default value in the function definition. So, in this case, if the message is missing in the arguments, the default message is considered. But if we are passing some message to the function, then the message that we are passing will be considered. In short, the default value will be considered only if we are not passing any value for that in the function. So, you can make use of the default arguments, as and when required. You can also give try some more similar examples so that you can get familiar with the concept.
Keyword arguments in Python
While creating a function, we specify some parameters, and while calling a function, we give the arguments accordingly to the function. While providing the arguments, if we are following the same order as the parameters, then these are the positional arguments. But, Let’s have a look at the below program, in which, we are again giving the positional arguments, but we do not get things as expected.
As you can see, in the above program, we still have given the positional arguments, but, this time, things go wrong. According to the position of argument 25, it is for the name, and “GyaniPandit” is for the age. So, as an output, we get something weird! Let’s have a look –
Hello 25, you are GyaniPandit years old.
As you can see, the output is quite unusual. But, this happened here, because the order does matter here. But we can use something called keyword arguments, in which, with the argument value, we specify for what variable are we giving this value. Let’s have a look at the below example, which corrects what went wrong in the above example –
Now, you can see that while we are calling the function, we are specifying that the value 25 is for the age, and the value “GyaniPandit” is for the name. So, this is how we specify the keyword arguments. You can make use of the keyword arguments, as and when required.
Arbitrary arguments (variable number of arguments)
Most of the time, when we are creating a function, we have an idea about how many arguments are needed to be passed to the function, and then we give the parameters accordingly. But sometimes, it may happen, that we do not have an idea, about how many arguments are going to be passed, and in such situations, we can have a variable number of arguments. Let’s understand this with an example here.
Let’s say that we are writing a function, where we take the name of the person as an argument, and after that, as arguments, we take the names of the fruit’s that the person likes. Now, some people may like 2 fruits, some might like 5 or some might like only 1. So, in this case, we are not sure how many arguments are we going to get. So, in such cases, we can make use of arbitrary arguments. In the function definition, we have to mention that we are going to have multiple arguments, and to do that, we are just going to use an asterisk mark, followed by the variable name. Have a look at the below example, which gives us an idea about the same.
As you can see, in the function definition, we first have given a simple argument, and after that, we have started using the asterisk mark, followed by the variable name, that we are going to have a variable number of arguments here. So, in the function call, you can simply see that we have given the first argument as the name of the person, and after that, there are some arguments, which are all names of fruits, that the person likes.
So, all the variable number of arguments would be stored as a tuple here, and we have to loop through the tuple, for the particular value. So, we are looping through the tuple, and for every fruit in the tuple, we are printing a message that the person like this fruit. Let’s have a look at the output now –
GyaniPandit likes apple
GyaniPandit likes pineapple
GyaniPandit likes orange
GyaniPandit likes mango
As you can see, for every fruit, we get a message that GyaniPandit likes some particular fruit.
So, you can make use of arbitrary arguments in such situations, when we do not know how many arguments are we going to have. You can also try printing just the fruit’s variable, and you would get a tuple as an output. You can also try some other similar examples, to get an idea about the arbitrary arguments.
Arbitrary keyword arguments in Python
We make use of keyword arguments in our functions. But sometimes, we are not sure about how many keyword arguments are we going to have, and in such situations, we can make use of arbitrary keyword arguments. Here, we are just going to give the keyword arguments, while calling the function. To specify that we are going to have a variable number of keyword arguments, we are going to make use of a double asterisk (**) followed by the variable name. Let’s have a look at an example, through which, we can understand the concept of a variable number of arguments –
As you can see, we are able to give a variable number of keyword arguments to the function. Notice that **details there. We are trying to print the details in the function, and you would find that we get a dictionary over here. So, all the keyword arguments that we are going to pass here, would be stored as key-value pairs in the dictionary. You can get the keys, and the corresponding values easily from the dictionary.
So, this was much about the functions here. We can make use of the functions, as and when required in the program. Functions are often very useful for us, to be used in the program. We are also familiar with some of the benefits of using the functions in our program. We also have addressed some of the different types of functions, on the basis of the arguments, and the returning values. You can also try to create some functions, in your programs, so that you can get familiar with the concept.