Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Default Parameters

00:00 Let’s take a look at how you, the author of a function, can assign default values to some or all of your parameters.

00:10 Parameters can be assigned values in the function headers—that’s the top line where the function definition starts. If, after a parameter name, you follow it with an equal sign (=) and then a value, that becomes the default value for that parameter and it becomes an optional parameter for someone trying to use or call your function.

00:34 If they don’t specify an argument for that particular parameter, then the one you supplied in the header is the one that’s going to be used. This gives us the notion of default parameters, or optional parameters.

00:48 Let’s take a look at defining our function—the same one we’ve been using before—in a slightly different way. We’re going to define our function f(). It’s going to have a parameter qty (quantity), and it’s going to have a default value of, say, 6. The second parameter we’ll still call item, but it will have a default parameter of 'bananas'.

01:11 And the third parameter we will call price and its default value will be 1.74.

01:19 The body of this function is going to be the same that we’ve seen before. We’re simply going to print qty, item, and then conclude the sentence and say that “Those many items cost…” and then express the price as a two-digit decimal number.

01:34 So, I can still use this function as I had before. I could call f() of 6, 'bananas', 1.74, and that shows up normally. I can say I have 4 'apples',

01:54 and they cost 2.24. And again, because I’m providing three arguments, all three parameters are getting their values from the function call.

02:03 But if I don’t say how much something costs,

02:09 before I would get an error because I did not supply enough arguments for a function that was expecting three, but now price has a default value.

02:19 And so if I don’t specify an argument value for that third parameter, its default value is going to be used. Likewise, let’s say there’s a sale and all of a sudden 7 bananas cost a $1.74.

02:36 If I just provide a parameter value for qty, item will use its default value 'bananas' and price will use its default value 1.74.

02:48 And because I have specified default values for all three parameters, I don’t have to supply any arguments! And in that case, all three of the parameters will get their values from the function header and they will use the default values.

03:08 And I can mix this with keyword argument passing. So let’s say I have item='apples' and the price=2.75. Here I haven’t provided a value for qty and so it’s going to use its default value.

03:30 When you are writing your function header, optional parameters must follow any required parameters. So once you start specifying optional parameters, you can’t then have a required parameter.

03:44 So, all of the required parameters come first and then any parameters where you want to provide default values would occur following that.

03:55 So, what have we learned about argument passing so far? Positional arguments must agree in order and in number with the parameters named in the header of the function definition.

04:06 If there are three given in the function header, then three must be supplied in the order that they’re expected. Keyword arguments must agree with the parameter names, but they can be used in any order. And of course, if you’re not using default parameters, they too must agree with the number of parameters a function is expecting.

04:28 Default parameters allow some arguments to be omitted when the function is called. Again, for values that often don’t change from one function call to the next but may sometimes be different, that would be a good use of a default parameter, a default value for that type of parameter.

04:48 When your parameter is going to hold a mutable object, there must be some care given to how you use default parameters, and the next lesson will provide an example of that.

Become a Member to join the conversation.