1. Default and keyword arguments
Now we know how to build custom functions, let's look at how to extend their capabilities!
2. Average
Recall our average function.
We briefly discussed that the variable, values, provided inside parentheses, was an argument.
3. Arguments
An argument might sound like a bad thing, but in a Python function or method, it refers to a value or data structure that is provided.
There are two types of arguments for functions and methods, positional and keyword arguments.
4. Positional arguments
We've been using positional arguments already, where we provide arguments in order, separating each by a comma.
The first positional argument is the first value included in the call of the function, the second positional argument is the second value, and so on.
An example is round, where we provide the value to be rounded, which would be the first positional argument. We then add a comma, followed by a number, representing the number of digits to round the value to, which is the second positional argument.
5. Keyword arguments
In contrast to positional arguments, keyword arguments require us to define each argument's value using its name keyword.
They help us understand what a function is doing as well as keep track if we have a function with lots of arguments.
To use keyword arguments, we include the keyword argument's name and set it equal to a value, such as a float.
Here, we call the round function again, this time providing the number keyword argument and setting it equal to pi with 10 decimal places.
6. Keyword arguments
We follow with a comma, then set the ndigits keyword argument equal to two.
We get the same result!
7. Identifying keyword arguments
But how do we know what these keywords are? We can use the help function to get this information!
Let's look at the output of calling help on the round function.
8. Keyword arguments
We can see the first argument is called number, which is the keyword for this argument.
The second keyword argument is called ndigits.
Notice that ndigits is set equal to None within the output?
9. Default arguments
In Python, None means no value, or empty.
This is an example of a default argument, which is a way of assigning a default value to an argument. We have been overwriting this argument's value from None to two, but the help output tells us that if ndigits is omitted the result will be rounded to zero decimal places, resulting in an integer.
10. Why have default arguments?
Default arguments are helpful as they force us to think about how people will use our functions. If we expect an argument to have a specific value the majority of the time then we can set it as a default argument.
It potentially reduces the amount of code required to run the function, if the default argument is not modified. Conversely, it maintains flexibility whereby users can overwrite the default value to return different results when calling a function!
11. Adding an argument
With this new knowledge, let's modify our previous average function, shown here.
12. Adding an argument
We add a keyword argument called rounded, with a default boolean value of False.
13. Adding an argument
Inside the function, we check if rounded has been changed to True.
If so, we calculate the average, round it to two decimal places, and then return this value.
14. Adding an argument
Otherwise, we calculate the average and return it without any rounding.
15. Using the modified average() function
Using our sales data from earlier, let's test our function.
16. Using the modified average() function
We can call the function using positional arguments, providing sales, then the boolean value of False so the results are not rounded.
Alternatively, as the default value of rounded is False, we can omit the rounded argument and the results will be the same!
17. Using the modified average() function
Likewise, we can use keyword arguments. Here, we set values equal to sales and rounded equal to True. This returns the average sales value rounded to two decimal places.
18. Let's practice!
Hopefully, now you can see the flexibility and potential of custom functions!