- [Instructor] In this video, I'm going to demonstrate
default parameter values.
Now, when you're defining functions in Python,
it's extremely common to provide default values
for each of the parameters you define
so that people can have the flexibility
of calling your functions a number of different ways.
So for the purpose of this example,
let's once again jump into ipython,
and let's define a new function.
We'll call it rectangle
area,
and for a rectangle's area,
we need to be able to calculate the length times the width,
so we need two different pieces of information
to perform that calculation.
So let's define a length parameter,
and just for argument's sake, no pun intended,
let's give the length a default value of two,
and let's have a width parameter as well,
and we'll give that a default value of three.
Now, when you're doing default arguments,
you typically wanna choose realistic values
that somebody would normally want as their default.
So you might use one for the length and one for the width,
or even zero for each of those.
That would be up to you as the definer of the function.
But we want to demonstrate the basic concept here,
so we chose some values that we probably
would not use normally.
And as we typically do,
we're going to give this a docstring,
so return a rectangle's area.
And the return statement for this is straightforward.
We simply want to return the length times the width.
So a relatively simple function definition in this case.
Now, because of the fact
that we've provided default arguments,
we actually have a couple of different ways
that we can call this function.
So we can say let's call rectangle area
with no arguments whatsoever.
Now there is no function defined that takes no arguments,
but there is a function defined with the same name
that has default values for every one of its parameters.
So this function call that I'm about to execute
is actually going to be treated
as if I pass in two for the length and three for the width.
So let's give that a shot and we see that we get six back.
Now, if I call rectangle area and let's say
I just pass it a single value, ten,
in this case, the Python interpreter is going to take
that value and use it as the first parameter
in the parameter list,
so the length will be ten,
and because we're not supplying a second parameter,
the width will be three.
So ten times three should give me 30,
and indeed it does.
Now of course we can always call rectangle area width
both arguments that we specified,
in which case it will simply use those two values
to calculate the results.
Now in the next video, we'll take a look at a concept
called keyword arguments and talk about other ways
in which we could call this same function.