-: Hi guys and welcome back.
In this video, we learn about
default parameter values in Python.
Let's get started.
We've got a function here called add
and it takes two arguments.
So it has two parameters, x and y.
And then it's gonna print
the result of adding them together.
So normally, you would call this add function
with two arguments.
So five comma eight.
Gives them five and eight to x and y.
However, you can have default values
for one or two of these parameters
by just saying in the function definition,
y equals eight.
Again, we usually don't put spaces around this equal sign
when we use it for a default parameter value
and what this is that x is a required parameter
and y is now an optional parameter.
If you don't give y a value when you call the function,
it will use the value eight,
which is defined here.
So, instead of doing add five comma eight,
you can just do add five.
And five will become the value for x,
since that is the first parameter and the first argument,
and because there is no second argument,
y will use its default value eight.
So if we run this code here,
you'll see that 13 comes out.
Similarly, you can do, of course,
x equal five, if you like,
and still y doesn't have a value,
so it will use the default value of eight.
So this means exactly the same thing.
What can't do though is do this, y equal five,
because x is a required parameter,
you need to give it a value,
and here, we're not given it,
so if we were to run this code,
you would get the add is missing
one required positional argument, x.
So you need to give x a value.
As usual, you can still do x equal five, y equal five,
that's totally fine.
And of course, you cannot do x equals five comma five,
because as we learned earlier on,
you can't put a positional argument after a keyword
or named argument.
Similarly, that also applies for
the add function definition.
You cannot have a default parameter
be followed by a non default parameter.
So here, it'll tell you
that a default value must be specified here,
in y, because you've specified one earlier on.
So default parameter values must go at the end,
after all the required non default parameters.
Something to take into consideration
when you are defining functions
is to usually not do this.
So here we've got a variable called default_y,
and it's equal to three.
And then we're defining an add function
and making the y parameter equal to our default_y variable.
So, y will equal three,
and that will be the default value for the y parameter.
Now we can call add with the value two, let's say,
and then we do for x and three for y,
giving us a total of five.
However, if we then go and change default y to four,
and then we add again, let's say add two,
now you'll see that actually,
we get the same value back from the function.
So changing a variable that has been used
as a default parameter value,
does not modify the function.
So this value here, this three,
is defined when the function gets created
and it doesn't change after that,
if you reassign the variable.
So this is why I don't usually recommend doing this,
because it can get really confusing really quickly.
So stick to putting the actual value on the function,
unless you really have to do something different,
and then make it clear that that value
is not going to change after defining the function.
So that is how you define
default parameter values in Python.
Just put them in the function definition,
just make sure that they are
at the end of the parameter lists.
Thank you for joining me in this video
and I'll see you in the next one.