In a previous lecture we learned about argument passing and now I want to talk about having default
arguments.
So let's say we had this example where AB function returns an integer and it calculates the volume of
a rectangular prison given the width length and height and all the function does is multiply the width
and height and returns it.
So in the main we have just three variables and we just set them all to five here and then we just pass
them into the function call.
And if it were an inch see the outputs volume is 125 K..
So if you wanted to you have a default parameter here.
Let's say we want to have the default of height to be 5 k.
So all we do is in the declaration of the function we set this right most parameter and it has to be
the rate most parameter for a reason not get into that later.
And we just said it to be equal to 5.
Notice that I'm not going to set the definition to be five here.
OK.
This is not needed at all.
In fact it's an error if you do it.
OK so it only happens in the declaration.
Now we don't actually need to send the height to the function call anymore.
OK because we already have it defaulted.
So if you run this again see it's still 125 even though we didn't set the height variable in the call
to the function.
In fact it could change the
default to be 10 and can rerun this and it's 250 Rantz five times five is twenty five times ten is 250
K.
So it's actually using the default here and even do you even have length beautyful so say length is
the default of two k and then we don't actually have to have length here.
And if you run this it's a it's five times two right.
It's ten times ten.
It's 100 days.
It's using the default length.
We could in fact have link here and right here it should be under-25 like it was before.
So you don't need to have or you don't have to have the length in height here but if you don't it'll
just default to do whatever you had here.
So but you can still call it just like normal if you want.
So knows here that we have the height redefault in order to have the length of the fault.
So otherwise the call to this function would be ambiguous it wouldn't understand what parameter you're
talking about.
You couldn't have something like this where length is not default which is OK.
You tried to run this it'll give you an error says error missing default argument on parameter length
and so saying so we're really saying the order in which you have defaults really matters and the way
I think of it is you always want to start from the rightmost parameter and work your way inwards.
Otherwise it's not going to work properly.
So this way.
So in the next section or in next lecture I'm going to talk about some looping with functions themselves.