Get startedGet started for free

Default and flexible arguments

1. Default and flexible arguments

Let's say that you're writing a function that takes multiple parameters and that there is often a common value for some of these parameters. In this case, you would like to be able to call the function without explicitly specifying every parameter. In other words, you would like some parameters to have default arguments that are used when it is not specified otherwise!

2. You'll learn:

In this video, you'll discover how to write function with default arguments, along with using flexible arguments, which allows you to pass any number of arguments to a function, as we'll soon see.

3. Add a default argument

First up, to define a function with a default argument value, in the function header we follow the parameter of interest with an equals sign and the default argument value. Notice that this function raises the first argument to the power of the second argument and the default 2nd argument value is 1. So we can call the function with two arguments as you would expect, however, if you only use one argument, the function call will use the default argument of 1 for the second parameter! Neat, huh? In the interactive exercises that follows, you'll gain expertise in writing functions with both single and multiple default arguments.

4. Flexible arguments: *args (1)

Lets now look at flexible arguments: let's say that you want to write a function but aren't sure how many arguments a user will want to pass it; for example, a function that takes floats or ints and adds them all up, irrespective of how many there are. Enter flexible arguments! In this example, we write the function that sums up all the arguments passed to it. In the function definition, we use the parameter star followed by args: this then turns all the arguments passed to a function call into a tuple called args in the function body; then, in the function body, to write our desired function, we initialize our sum sum_all to 0, loop over the tuple args and add each element of it successively to sum_all and then return it.

5. Flexible arguments: *args (2)

We can now call our function add_all with any number of arguments to add them all up!

6. Flexible arguments: **kwargs

You can also use a double star to pass an arbitrary number of keyword arguments, also called kwargs, that is, arguments preceded by identifiers. We'll write such a function called print_all that prints out the identifiers and the parameters passed to them as you see here.

7. Flexible arguments: **kwargs

Now to write such a function, we use the parameter kwargs preceded by a double star. This turns the identifier-keyword pairs into a dictionary within the function body. Then, in the function body all we need to do is to print all the key-value pairs stored in the dictionary kwargs. Note that it is NOT the names args and kwargs that are important when using flexible arguments, but rather that they're preceded by a single and double star, respectively.

8. Let's practice!

I know this is a lot to take in so it's now time to go and hack it out yourself. Get writing functions!