1
00:00:04,340 --> 00:00:08,480
Hey what's going on everyone this is Jacob with Deb slopes dot com.

2
00:00:08,610 --> 00:00:16,080
And today I'm going to just do a short video on how recursion and what you would use you know recursive

3
00:00:16,080 --> 00:00:23,430
functions for and what it is exactly recurs in like the textbook definition for it is recursion is the

4
00:00:23,430 --> 00:00:27,570
process of repeating items in a self similar way.

5
00:00:27,570 --> 00:00:33,860
So something that you would use like a loop for or any kind of iteration like that.

6
00:00:34,050 --> 00:00:41,110
You could probably use a form of recursion to solve the same exact problem.

7
00:00:41,130 --> 00:00:47,250
One thing you want to think of with recursion though is it is very expensive when it comes to memory.

8
00:00:47,250 --> 00:00:55,290
So if you're calling the function more than a thousand times you probably don't want to use a recursive

9
00:00:55,290 --> 00:00:56,100
function.

10
00:00:56,220 --> 00:00:58,180
But I'm going to show you some examples.

11
00:00:58,200 --> 00:01:03,760
And let's go ahead and open up playground

12
00:01:06,380 --> 00:01:06,960
Okay.

13
00:01:06,960 --> 00:01:20,340
So I just opened up the X code playground will make the screen bigger.

14
00:01:20,420 --> 00:01:22,860
That should be big.

15
00:01:23,370 --> 00:01:29,240
I'm jumping into a simple example here using a for loop and then we'll show you how you can solve the

16
00:01:29,240 --> 00:01:33,460
same thing with recursion just to get our feet wet.

17
00:01:33,620 --> 00:01:40,940
This isn't the best example because the purpose for using recursion is to usually like make your code

18
00:01:40,940 --> 00:01:42,570
cleaner and easier to read.

19
00:01:42,680 --> 00:01:49,910
And you know with swift these days the way that a foreign loop is now written it's pretty clean and

20
00:01:49,910 --> 00:01:53,000
easy to read but we'll do we'll do it anyways.

21
00:01:53,360 --> 00:01:55,910
Just so you can understand what's going on.

22
00:01:56,060 --> 00:01:58,870
So if we just write a function here.

23
00:01:59,060 --> 00:02:03,290
Print numbers we're just going to print numbers to the screen.

24
00:02:07,120 --> 00:02:22,450
And we're going to make a simple for loop for x in 0 8 Max and we're just going to simply print print

25
00:02:22,540 --> 00:02:23,140
X

26
00:02:27,670 --> 00:02:33,730
and let's open up the bottom here and now we will say print numbers.

27
00:02:34,000 --> 00:02:42,520
And why don't we just toss in three and you know like you're familiar with you get your count 0 1 through

28
00:02:42,520 --> 00:02:43,510
3.

29
00:02:43,510 --> 00:02:43,770
All right.

30
00:02:43,780 --> 00:02:50,940
So how do we do this with using a recursive function call.

31
00:02:50,960 --> 00:02:59,420
It's pretty easy so let's write a function here and we're going to call this print numbers rack for

32
00:02:59,630 --> 00:03:06,720
recursive and we can pass in another integer the same exact way as above.

33
00:03:07,040 --> 00:03:12,590
And here we just have to make sure the max is above zero

34
00:03:16,200 --> 00:03:17,690
and right now right.

35
00:03:17,700 --> 00:03:21,180
So we're going to call our function.

36
00:03:21,190 --> 00:03:30,730
So so a function that calls itself from within itself that's a recursive function.

37
00:03:30,870 --> 00:03:38,040
And what we're going to do here is we're actually going to call print numbers rack and we're going to

38
00:03:38,040 --> 00:03:44,480
pass in the variable that already was passed in which is Max.

39
00:03:44,610 --> 00:03:47,300
But we're going to subtract one from it.

40
00:03:47,700 --> 00:03:53,050
And this kind of as you can see it's kind of the same concept as a loop already.

41
00:03:53,760 --> 00:04:02,100
So we can come down here outside of the if statement and print Max and it's going to iterate through

42
00:04:02,100 --> 00:04:08,400
the same exact way for the for loop did when we call.

43
00:04:08,400 --> 00:04:16,800
So if we do print numbers wreck and let's pass for and just to show a little different.

44
00:04:16,950 --> 00:04:17,460
There we go.

45
00:04:17,460 --> 00:04:21,640
So it just printed 0 1 2 3 4.

46
00:04:21,900 --> 00:04:25,580
So this function is the same exact function is up here.

47
00:04:25,680 --> 00:04:32,940
And like I said this isn't the best example of like why you would use a recursive call function only

48
00:04:32,940 --> 00:04:38,210
because it's it's the same amount of code if not more.

49
00:04:39,210 --> 00:04:46,990
But it's a good it's a good easy example to wrap your head around what recursion is.

50
00:04:47,370 --> 00:04:51,130
So I'm going to go ahead and delete this OK.

51
00:04:51,140 --> 00:04:59,410
Now we can do a little bit more of a not really difficult recursive call but this is exactly the reason

52
00:04:59,410 --> 00:05:05,610
why you'd probably want to use reoccurs or an over like looping through a for loop.

53
00:05:05,680 --> 00:05:10,930
And what we're going to do is we're going to find factorials.

54
00:05:11,230 --> 00:05:19,930
And if you're not familiar with what a factorial of non-negative number of Campeon negative a non-negative

55
00:05:19,930 --> 00:05:20,710
integer

56
00:05:24,350 --> 00:05:27,510
if you're not familiar with that we'll just go over that real quick.

57
00:05:27,510 --> 00:05:31,850
So I'm in back back to middle school math.

58
00:05:31,980 --> 00:05:35,570
So a factorial is shown by the explanation point.

59
00:05:35,760 --> 00:05:39,460
If you remember this like what would be the factorial of 1.

60
00:05:39,520 --> 00:05:45,470
Well that is just going to be equal to 1 if we wanted to that and all of the factorial of 2.

61
00:05:45,510 --> 00:05:53,700
It's 1 1 times 2 and that would equal two.

62
00:05:54,050 --> 00:06:05,680
And then as you go up factorials three would be one times two times three and that equals six.

63
00:06:05,680 --> 00:06:12,110
All right so I mean obviously like for smaller numbers this is really easy to do.

64
00:06:12,550 --> 00:06:19,870
But you know already when you get to 7 you're you know you're doing one times two times three times

65
00:06:19,870 --> 00:06:30,100
for five six and seven that can get pretty tedious and you start getting large numbers here.

66
00:06:30,130 --> 00:06:33,750
So that's five thousand forty.

67
00:06:33,850 --> 00:06:42,430
Now if we were to do this the old like not the old fashioned way but if we were to do this with a four

68
00:06:42,430 --> 00:06:47,900
inch loop we end up writing a lot more code and we're going to do that.

69
00:06:47,920 --> 00:06:55,140
I'm going to write the for loop just so you can see the benefits of using recursion.

70
00:06:55,150 --> 00:07:06,940
So let's see we need of a variable first that is going to keep track of our multiplication solutions

71
00:07:06,960 --> 00:07:11,520
so we'll just set that to zero and then let's make our 4 and loop.

72
00:07:12,070 --> 00:07:13,970
And we want this one.

73
00:07:13,990 --> 00:07:21,960
We don't want zero in there so 1 through whatever number we pass in.

74
00:07:22,180 --> 00:07:38,140
And then if the factorial is not equal to zero we are going to set the factorial variable equal to what

75
00:07:39,240 --> 00:07:48,820
value is already stored in our variable times X times the iteration that we're going through and our

76
00:07:48,820 --> 00:07:51,290
for in loop.

77
00:07:51,790 --> 00:08:01,000
Now if it is zero we need to set our factorial variable to 1

78
00:08:04,490 --> 00:08:11,120
and then we can go ahead and return our factorial variable.

79
00:08:11,150 --> 00:08:16,090
So you know it's not a lot of code but it's still a decent amount.

80
00:08:16,130 --> 00:08:18,370
And why don't we just test this.

81
00:08:18,530 --> 00:08:25,580
We already know the factorial of 7 Let's see where is my size.

82
00:08:25,580 --> 00:08:29,770
So the factorial of seven was five thousand forty.

83
00:08:29,770 --> 00:08:31,500
So that worked.

84
00:08:31,550 --> 00:08:33,340
This solution works for us.

85
00:08:33,740 --> 00:08:35,720
OK so if we want to use this with

86
00:08:38,430 --> 00:08:42,680
recursion we have to think through what we want to do.

87
00:08:42,750 --> 00:08:50,160
We want to take the number that we're passing in and we have to multiply everything from 1 to that number

88
00:08:50,160 --> 00:08:53,600
the number we pass on is the max.

89
00:08:53,940 --> 00:08:58,200
So let's think this through we can write the basic

90
00:09:02,580 --> 00:09:03,420
function.

91
00:09:03,460 --> 00:09:18,330
So we're passing an integer and we also want to return an integer just like before.

92
00:09:18,360 --> 00:09:21,780
So so far everything is the same.

93
00:09:21,930 --> 00:09:32,630
Now at some point we want to return the number that is being passed in times you know every single number.

94
00:09:32,630 --> 00:09:38,310
So from you know 7 to 1.

95
00:09:38,830 --> 00:09:49,720
So it's pretty easy we just had to call call our factorial function and pass in the number again but

96
00:09:49,720 --> 00:09:51,210
minus 1.

97
00:09:51,220 --> 00:10:05,330
So just like the how we iterated through the numbers and printed 0 through 3 before it's the same concept.

98
00:10:05,380 --> 00:10:14,020
There's one problem with this though if we try to call this it's not going to work very well so we probably

99
00:10:14,020 --> 00:10:25,390
have to think about this like OK like up above we have to make sure it's above zero or we're all like

100
00:10:25,630 --> 00:10:30,780
one times one or one times anything is the same number.

101
00:10:30,940 --> 00:10:36,530
So maybe we should just check for if the number is equal to 1.

102
00:10:36,910 --> 00:10:38,280
We can return one.

103
00:10:38,320 --> 00:10:46,550
And that'll take care of you know the smaller numbers as we approach one or it won't allow us to go

104
00:10:46,550 --> 00:10:48,370
negative either.

105
00:10:49,040 --> 00:10:50,600
But then we need to

106
00:10:55,000 --> 00:11:02,480
return this Whoops sorry about that bracket.

107
00:11:03,460 --> 00:11:04,220
Right.

108
00:11:05,480 --> 00:11:10,480
Cut and paste.

109
00:11:10,500 --> 00:11:17,940
All right so if we pass on the same number now to our factorial let's pass and seven and we should get

110
00:11:17,940 --> 00:11:21,130
five thousand forty again on the right side here.

111
00:11:21,270 --> 00:11:22,140
Yes so we did.

112
00:11:22,140 --> 00:11:28,800
So this was called six times starting with it too.

113
00:11:28,800 --> 00:11:36,450
So we basically ignored one because we know that one times anything is going to be the same exact number

114
00:11:36,750 --> 00:11:43,690
and that prevents us from going into the negative numbers if we hit zero or negative one function one

115
00:11:43,750 --> 00:11:44,420
at work.

116
00:11:44,430 --> 00:11:46,460
And yes.

117
00:11:46,530 --> 00:11:49,630
So we would all ran into the same problem here.

118
00:11:49,770 --> 00:11:58,830
But so the point of recursion we have one two three four five six basically seven lines of code finding

119
00:11:58,830 --> 00:12:08,100
the factorial with recursion versus 1 2 3 4 5 6 7 8 versus 11 lines of code.

120
00:12:08,100 --> 00:12:13,160
So it makes it a lot easier to read.

121
00:12:13,350 --> 00:12:19,000
When I first learned about recursion I wanted to use it in every scenario possible.

122
00:12:19,050 --> 00:12:25,170
It's not that realistic but there are certain circumstances where it works out perfect and it can be

123
00:12:25,530 --> 00:12:26,790
a pretty powerful tool.

124
00:12:26,790 --> 00:12:28,080
It's cool.

125
00:12:28,110 --> 00:12:28,740
All right.

126
00:12:28,740 --> 00:12:34,140
So obviously like with work with recursive calls it can get really confusing.

127
00:12:34,170 --> 00:12:42,470
I try to use really basic simple examples for today because they can get very confusing.

128
00:12:43,470 --> 00:12:44,240
But a good way.

129
00:12:44,240 --> 00:12:52,430
A good place to practice where you can use a lot of these skills and practice using it as Project ular

130
00:12:52,430 --> 00:12:54,360
dot dot net.

131
00:12:54,360 --> 00:12:56,100
I like to go here.

132
00:12:56,170 --> 00:13:03,120
They have awesome little like mind exercises it makes me start thinking more like a programmer and like

133
00:13:03,120 --> 00:13:04,610
solving problems.

134
00:13:04,710 --> 00:13:06,570
And it's pretty cool.

135
00:13:06,570 --> 00:13:12,000
You would log in and you just go through you go and order you know one through whatever.

136
00:13:12,000 --> 00:13:19,200
And after the first two after the first three this gets pretty darned difficult.

137
00:13:19,260 --> 00:13:26,380
But you can use like recursive function calls and a lot of these problems to make them easier to solve.

138
00:13:26,580 --> 00:13:28,890
And I would definitely recommend checking it out.

139
00:13:28,890 --> 00:13:34,260
That's project ILIR dot net and it's awesome.

140
00:13:34,440 --> 00:13:38,730
And my name is Jacob Deb soap's dot com and I hope you enjoy the video.
