1 00:00:06,060 --> 00:00:11,250 All right, so let's implement our recursive factorial function. 2 00:00:11,610 --> 00:00:16,140 So we will say cargo, new recursion. 3 00:00:16,410 --> 00:00:18,120 We'll go into recursion. 4 00:00:19,050 --> 00:00:24,210 And now we will open up our main address. 5 00:00:25,170 --> 00:00:33,480 So to keep it similar to how we talked about it in the previous lecture, we will make our own function 6 00:00:34,440 --> 00:00:46,590 called fact and we'll make our argument a i 32 num and we want to return an I 32 as well. 7 00:00:47,610 --> 00:00:47,880 All right. 8 00:00:47,880 --> 00:00:55,110 So we have a base case that we can look for in here and we want to make sure that num is greater than 9 00:00:55,110 --> 00:01:01,080 one, because if it's not greater than one, well then there's nothing else to do. 10 00:01:01,080 --> 00:01:05,580 So we can just flat out return one. 11 00:01:06,210 --> 00:01:14,250 Otherwise we want to return num times, fact of num minus one. 12 00:01:15,480 --> 00:01:27,450 And if you remember we do this because we have in factorial is equal to n times n minus one factorial. 13 00:01:27,450 --> 00:01:36,300 And we establish that this is equal to calling our factorial of n function and having it call itself 14 00:01:36,300 --> 00:01:36,630 again. 15 00:01:36,630 --> 00:01:46,440 So the recursion happens here of the result of n times factorial of n minus one, and that's exactly 16 00:01:46,470 --> 00:01:47,010 what we're doing. 17 00:01:47,010 --> 00:01:55,290 So we're passing in our parameter or our argument and we're saying we want to return that number times 18 00:01:55,290 --> 00:01:58,380 the factorial of num minus one. 19 00:01:58,800 --> 00:02:06,510 So to test this, we can come down here and we can print line 20 00:02:09,840 --> 00:02:16,710 and we just want to print out the factorial of five. 21 00:02:18,180 --> 00:02:24,810 And if we go run this, we will see that we have 120, which is correct. 22 00:02:24,810 --> 00:02:34,110 That is five times four times three times two, five times four, 20 times three, 60 times two 120. 23 00:02:35,010 --> 00:02:37,500 And that's all there is to it, to recursion. 24 00:02:37,500 --> 00:02:37,830 Right. 25 00:02:37,830 --> 00:02:45,390 So it's a it's a pretty simple concept and we want to make sure that. 26 00:02:46,580 --> 00:02:52,820 For it to be recursion that we just have to call ourselves. 27 00:02:53,060 --> 00:02:57,100 So we're going to look at a couple more examples of recursion. 28 00:02:57,110 --> 00:03:03,680 That way you get really comfortable doing it so that as we use it later on in this course, you are 29 00:03:03,680 --> 00:03:09,500 fully confident in your ability to understand what is what is going on.