WEBVTT

00:00.150 --> 00:07.050
In a previous lecture we learned about argument passing and now I want to talk about having default

00:07.080 --> 00:08.080
arguments.

00:08.340 --> 00:16.590
So let's say we had this example where AB function returns an integer and it calculates the volume of

00:16.620 --> 00:25.620
a rectangular prison given the width length and height and all the function does is multiply the width

00:26.010 --> 00:28.230
and height and returns it.

00:28.230 --> 00:36.150
So in the main we have just three variables and we just set them all to five here and then we just pass

00:36.150 --> 00:38.940
them into the function call.

00:39.180 --> 00:47.110
And if it were an inch see the outputs volume is 125 K..

00:47.180 --> 00:52.770
So if you wanted to you have a default parameter here.

00:52.960 --> 01:00.210
Let's say we want to have the default of height to be 5 k.

01:00.420 --> 01:13.620
So all we do is in the declaration of the function we set this right most parameter and it has to be

01:13.620 --> 01:18.040
the rate most parameter for a reason not get into that later.

01:18.390 --> 01:20.910
And we just said it to be equal to 5.

01:20.910 --> 01:27.430
Notice that I'm not going to set the definition to be five here.

01:28.460 --> 01:28.880
OK.

01:28.900 --> 01:32.900
This is not needed at all.

01:32.930 --> 01:35.570
In fact it's an error if you do it.

01:36.040 --> 01:41.010
OK so it only happens in the declaration.

01:41.060 --> 01:47.730
Now we don't actually need to send the height to the function call anymore.

01:48.900 --> 01:51.030
OK because we already have it defaulted.

01:51.070 --> 02:02.060
So if you run this again see it's still 125 even though we didn't set the height variable in the call

02:02.690 --> 02:04.100
to the function.

02:04.120 --> 02:05.780
In fact it could change the

02:08.590 --> 02:18.190
default to be 10 and can rerun this and it's 250 Rantz five times five is twenty five times ten is 250

02:18.810 --> 02:19.230
K.

02:19.390 --> 02:29.440
So it's actually using the default here and even do you even have length beautyful so say length is

02:29.440 --> 02:38.910
the default of two k and then we don't actually have to have length here.

02:39.030 --> 02:45.180
And if you run this it's a it's five times two right.

02:45.220 --> 02:46.580
It's ten times ten.

02:46.580 --> 02:48.250
It's 100 days.

02:48.250 --> 02:51.530
It's using the default length.

02:51.810 --> 03:01.690
We could in fact have link here and right here it should be under-25 like it was before.

03:02.020 --> 03:12.040
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

03:12.070 --> 03:17.170
just default to do whatever you had here.

03:19.610 --> 03:26.480
So but you can still call it just like normal if you want.

03:26.980 --> 03:35.860
So knows here that we have the height redefault in order to have the length of the fault.

03:36.180 --> 03:43.440
So otherwise the call to this function would be ambiguous it wouldn't understand what parameter you're

03:43.440 --> 03:44.100
talking about.

03:44.100 --> 03:53.040
You couldn't have something like this where length is not default which is OK.

03:53.130 --> 03:59.890
You tried to run this it'll give you an error says error missing default argument on parameter length

04:00.630 --> 04:09.480
and so saying so we're really saying the order in which you have defaults really matters and the way

04:09.480 --> 04:18.480
I think of it is you always want to start from the rightmost parameter and work your way inwards.

04:18.540 --> 04:20.790
Otherwise it's not going to work properly.

04:21.690 --> 04:26.480
So this way.

04:26.840 --> 04:37.040
So in the next section or in next lecture I'm going to talk about some looping with functions themselves.
