WEBVTT

00:00.110 --> 00:03.590
So next up let's talk about the recursion.

00:03.590 --> 00:06.530
And for that we're gonna calculate the factorial.

00:06.560 --> 00:11.780
Now every function that's recursive needs to have a base case.

00:11.780 --> 00:15.440
That's a condition that basically stops the recursion.

00:15.440 --> 00:21.500
And that's the simplest instance of the problem that can be solved directly.

00:21.500 --> 00:24.440
This is how this can be technically described.

00:24.440 --> 00:32.690
And in the case of factorial, when you calculate factorial of zero or factorial of one, the result

00:32.690 --> 00:34.160
is always one.

00:34.160 --> 00:37.100
So this basically returns from the function.

00:37.100 --> 00:43.880
But then since the function is recursive, there needs to be a recursive case.

00:44.540 --> 00:48.260
That's the case when the function calls itself.

00:48.260 --> 00:57.290
And in case of factorial, everything above one is calling itself, but with an argument minus one.

00:57.290 --> 00:59.480
So this is how factorial works.

00:59.480 --> 01:02.900
Was this is a recursive function.

01:05.330 --> 01:11.100
So for example, if we call factorial with the argument five.

01:11.460 --> 01:16.290
Then you can see that clearly five is not zero or it isn't one.

01:17.400 --> 01:27.750
So it will multiply five by factorial of four, and so on and so on up until the factorial will receive

01:27.750 --> 01:30.330
either 1 or 0 as an argument.

01:30.360 --> 01:33.690
Then the final result will be returned.

01:34.860 --> 01:37.050
So in this case this is 120.

01:42.240 --> 01:44.880
So let's do an actual example.

01:44.880 --> 01:46.500
Let's run this code.

01:47.250 --> 01:50.070
Let me create a recursive PHP file.

01:50.130 --> 01:53.250
And let's type this code.

01:54.960 --> 01:56.280
So we need a starting tag.

01:56.280 --> 02:02.970
And we need the function called factorial which accepts n as a parameter.

02:03.000 --> 02:06.570
Let's also type that and it also returns an int.

02:06.570 --> 02:08.700
So we start with a base case.

02:09.660 --> 02:16.620
So base case is n is either zero or n is one.

02:18.090 --> 02:19.610
So that's our base case.

02:19.610 --> 02:23.990
That's the case that would let this function return.

02:23.990 --> 02:26.390
That will stop the recursion.

02:26.810 --> 02:36.170
Now there also needs to be a recursive case when we actually call factorial within factorial.

02:36.770 --> 02:40.880
So we're gonna multiply n by.

02:40.910 --> 02:43.430
Sorry that's not a variable by factorial.

02:43.460 --> 02:46.430
Passing n minus one.

02:47.750 --> 02:56.300
Let's reformat this now so we can actually see what argument was called and when.

02:56.840 --> 02:59.330
Let's echo.

03:01.520 --> 03:05.780
N and add a new line.

03:06.950 --> 03:14.600
Finally let's um let's run this function using var dump.

03:14.900 --> 03:18.530
Let's call factorial passing five.

03:18.530 --> 03:24.620
And what I expect is I expect to see every single time this function is being called.

03:24.620 --> 03:26.870
And with what argument.

03:28.680 --> 03:32.790
So we are running the recursive PHP file.

03:33.510 --> 03:36.510
So the first call is with the argument five.

03:36.510 --> 03:40.080
So we know that five is not 0 or 1.

03:40.230 --> 03:42.450
So this is causing the recursion.

03:42.450 --> 03:45.330
It's calling factorial again with four.

03:46.560 --> 03:48.900
So obviously four is also none of this.

03:48.900 --> 03:51.660
So it calls factorial with three.

03:51.660 --> 03:55.470
And then there is another nested call with two.

03:56.490 --> 04:03.600
And finally when factorial is called with one it returns one.

04:03.600 --> 04:06.390
So it doesn't call factorial again.

04:06.420 --> 04:13.590
It breaks the recursion so that all those nested factorial calls can also return the actual value,

04:13.590 --> 04:18.600
because now the actual value, the the row number is known.

04:18.600 --> 04:28.140
So then all of those nested factorial functions return up until the main factorial call, which can

04:28.140 --> 04:35.100
also return a value to the caller, which is this line, and this returns this value.
