Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,630 --> 00:00:04,020
So now how we write a recursive function for it.
2
00:00:04,470 --> 00:00:11,310
So let's just write down what we are looking for in the comments here and we are going to type that.
3
00:00:11,310 --> 00:00:15,260
We are going to find the sum of one plus two plus three plus.
4
00:00:15,300 --> 00:00:17,550
So on up until a given num.
5
00:00:17,670 --> 00:00:17,920
Right.
6
00:00:17,940 --> 00:00:24,180
So the function will get these num as a parameter and it will return in a recursive manner.
7
00:00:24,480 --> 00:00:27,990
The sum from one up to these given num.
8
00:00:28,440 --> 00:00:33,080
So we know that the function will receive is a parameter or some integer.
9
00:00:33,090 --> 00:00:33,360
Right.
10
00:00:33,390 --> 00:00:35,160
In this case NUM is an integer.
11
00:00:35,400 --> 00:00:37,140
So we know it will be an integer.
12
00:00:37,150 --> 00:00:43,200
And also we know that the sum, a sum of indigenous is also expected to be of an integer type.
13
00:00:43,320 --> 00:00:46,350
So that's why we are using the function type as.
14
00:00:46,690 --> 00:00:47,350
So end.
15
00:00:47,420 --> 00:00:49,910
And let's call this function as we called it previously.
16
00:00:50,200 --> 00:00:52,510
Sum up two.
17
00:00:52,710 --> 00:00:53,090
Okay.
18
00:00:53,250 --> 00:00:56,890
And in the parameters, we are going to specify it now.
19
00:00:57,210 --> 00:00:58,410
So I hope that's clear.
20
00:00:58,410 --> 00:01:02,450
So far we just created our signature for these function.
21
00:01:02,850 --> 00:01:08,610
And knowing the curly brackets, we are going to specify how these recursive function will work to find
22
00:01:08,610 --> 00:01:09,920
out these some.
23
00:01:10,320 --> 00:01:14,490
And we know that the sum of all the numbers from one up to NUM.
24
00:01:14,790 --> 00:01:16,600
We can say that it will be equal to.
25
00:01:16,620 --> 00:01:17,610
We said it previously.
26
00:01:17,730 --> 00:01:22,080
Just a quick reminder to the sum of one plus two plus three plus.
27
00:01:22,140 --> 00:01:23,730
So on right.
28
00:01:23,790 --> 00:01:29,400
Glass num minus one num minus one glass.
29
00:01:29,400 --> 00:01:32,770
And we separated as just num.
30
00:01:32,960 --> 00:01:36,570
OK, so it's the same sum we just separating it.
31
00:01:36,600 --> 00:01:42,870
That are the total something, the bigger problem equals to some smaller prob problem.
32
00:01:42,870 --> 00:01:43,170
Right.
33
00:01:43,470 --> 00:01:46,380
The sum up to num minus one.
34
00:01:46,470 --> 00:01:49,300
All of these, some plus of these given num.
35
00:01:49,430 --> 00:01:49,580
OK.
36
00:01:49,680 --> 00:01:53,340
We just kind of kind of extracted from.
37
00:01:53,410 --> 00:02:00,000
Are it a problem that is less than the given problem that the big problem.
38
00:02:00,010 --> 00:02:00,170
OK.
39
00:02:00,510 --> 00:02:02,140
So we have the big problem.
40
00:02:02,190 --> 00:02:02,490
OK.
41
00:02:02,580 --> 00:02:04,380
This sum from one up to num.
42
00:02:05,050 --> 00:02:11,970
When we divided, as we said previously in the introduction videos, we divided with split it into some
43
00:02:12,030 --> 00:02:17,640
smaller problem right from one up to now minus one plus these given.
44
00:02:17,830 --> 00:02:18,070
Okay.
45
00:02:18,270 --> 00:02:26,040
So we created a little sub problem from these given problem and now we want to see how we can specify.
46
00:02:26,070 --> 00:02:31,620
So basically we know that a recursive function is just the function that calls itself right.
47
00:02:31,650 --> 00:02:39,480
And we know that a function some up to knows how to find the sum up to a given num that it receives
48
00:02:39,480 --> 00:02:43,260
here up to a given num from one up to these given num num.
49
00:02:43,650 --> 00:02:47,090
So we will say that it these function will return.
50
00:02:47,250 --> 00:02:47,520
Right.
51
00:02:47,580 --> 00:02:49,210
It will return num.
52
00:02:49,680 --> 00:02:55,800
Plus we said num plus this sum up to num minus one as you can see here.
53
00:02:56,100 --> 00:03:01,530
So it will return the num plus sum up to num minus one.
54
00:03:01,650 --> 00:03:01,960
Okay.
55
00:03:02,220 --> 00:03:10,880
So we extracted these num, we extracted it and we are using a recursive function call to a sub problem
56
00:03:10,920 --> 00:03:17,010
where we are going to call and find the sum up to this num minus one.
57
00:03:17,450 --> 00:03:17,740
Okay.
58
00:03:18,030 --> 00:03:22,410
So if we are going to run this function right now, let's give it a try.
59
00:03:22,560 --> 00:03:24,510
For example, we will initialize.
60
00:03:25,220 --> 00:03:26,100
We will let.
61
00:03:26,160 --> 00:03:28,640
Let's just declare it so int num.
62
00:03:28,920 --> 00:03:33,720
And we know that we will bring some nice message to the screen.
63
00:03:33,750 --> 00:03:43,170
Enter your number and then these are will insert his number and let's use instead of enter insert your
64
00:03:43,170 --> 00:03:43,690
numbers.
65
00:03:43,740 --> 00:03:50,760
So now we are going to use Skåne F percentage the and relating to now.
66
00:03:51,450 --> 00:03:58,980
And we want to receive the final result from these functions sum up to given num returns a result.
67
00:03:59,010 --> 00:03:59,280
Right.
68
00:03:59,490 --> 00:04:02,490
So we will create another variable and college result.
69
00:04:02,910 --> 00:04:04,760
And here we will call these functions.
70
00:04:04,800 --> 00:04:05,970
So result equals.
71
00:04:06,030 --> 00:04:09,360
To sum up to this given number.
72
00:04:09,810 --> 00:04:15,420
So for example, if the NUM was three, then these function we expected to return six.
73
00:04:15,420 --> 00:04:15,670
Right.
74
00:04:15,690 --> 00:04:17,010
One plus two plus three.
75
00:04:17,150 --> 00:04:19,820
And now let's try to print out the results.
76
00:04:19,830 --> 00:04:21,950
So print f resolved.
77
00:04:22,440 --> 00:04:23,870
Equals two percentage.
78
00:04:24,050 --> 00:04:24,410
Right.
79
00:04:24,510 --> 00:04:28,380
And we are going to just see what the result is.
80
00:04:28,470 --> 00:04:30,180
So let's build and run it.
81
00:04:30,210 --> 00:04:33,600
So build and run and and enter your number.
82
00:04:33,720 --> 00:04:36,420
We are going to insert three, for example.
83
00:04:36,450 --> 00:04:37,560
And let's see what happens.
84
00:04:37,980 --> 00:04:39,990
And basically nothing happens.
85
00:04:40,020 --> 00:04:43,110
And it seems that our program get stack.
86
00:04:43,140 --> 00:04:43,380
OK.
87
00:04:43,440 --> 00:04:46,860
We received some value that we didn't even expect.
88
00:04:47,250 --> 00:04:49,140
Something something went wrong here.
89
00:04:49,170 --> 00:04:49,440
Right.
90
00:04:49,470 --> 00:04:55,140
We expected that there will be just a message with the result equals two six.
91
00:04:55,320 --> 00:04:57,780
And we got something very, very strange here.
92
00:04:58,300 --> 00:04:59,340
And let's.
93
00:04:59,550 --> 00:05:02,520
Since right to understand what was the problem.
94
00:05:02,700 --> 00:05:10,230
So what we are doing here in this line, we are calling this some up to these given Nahm and given here
95
00:05:10,320 --> 00:05:11,640
a number of threes.
96
00:05:11,670 --> 00:05:17,100
So some up to three will return three plus some up to two.
97
00:05:17,430 --> 00:05:22,470
And this recursive call calls another instance of this function with the number two.
98
00:05:22,500 --> 00:05:27,120
So sum up to two returns, two plus sum up to one.
99
00:05:27,330 --> 00:05:29,340
And there you go again and again and again.
100
00:05:29,910 --> 00:05:36,810
And it's kind of an infinity of infinite recursive function call.
101
00:05:36,900 --> 00:05:39,380
There is no stopping condition.
102
00:05:39,660 --> 00:05:45,120
We just will call it for a sum up to zero, sum up to minus one, sum up to minus two and so on.
103
00:05:45,330 --> 00:05:46,770
There is no end to it.
104
00:05:47,160 --> 00:05:53,020
And the function will eventually not return as anything because it will never stop.
105
00:05:53,040 --> 00:05:55,220
It will never reach some stopping condition.
106
00:05:55,320 --> 00:05:58,630
So that's why we are going to use our base case.
107
00:05:58,680 --> 00:05:58,920
OK.
108
00:05:58,980 --> 00:06:04,990
We know that there should be in the recursive function, some base case, some stopping condition.
109
00:06:05,010 --> 00:06:08,580
That tells when we should stop the recursive calls.
110
00:06:08,730 --> 00:06:16,230
And in this case, it will be simply when numb when these given num sum up to where and a num equals
111
00:06:16,230 --> 00:06:19,730
to one because we know that it's straightforward.
112
00:06:19,770 --> 00:06:22,590
That sum up to one is just one.
113
00:06:22,710 --> 00:06:22,990
OK.
114
00:06:23,430 --> 00:06:30,150
And we will use this condition if num if num equals for example equals to one.
115
00:06:30,330 --> 00:06:36,510
Then in this case we know that there is a trivial solution, a trivial resolve that we can return from
116
00:06:36,510 --> 00:06:39,090
this function and it will be return.
117
00:06:39,180 --> 00:06:41,640
Return are just one.
118
00:06:42,030 --> 00:06:42,270
OK.
119
00:06:42,510 --> 00:06:50,490
And I'm not using here if Nahm is less than one, because we we will assume that this number that the
120
00:06:50,490 --> 00:06:54,420
user provides, that this function will be greater or equals to one.
121
00:06:54,540 --> 00:07:00,240
So at this point, we know that there is our stopping condition, as it will when we reach one.
122
00:07:00,270 --> 00:07:03,750
So we will have here three function calls.
123
00:07:04,020 --> 00:07:08,430
So the first function call will be with NUM equals two three.
124
00:07:09,060 --> 00:07:12,660
And the function will return, will return three.
125
00:07:12,660 --> 00:07:18,090
Plus are some sum up to up to two.
126
00:07:18,270 --> 00:07:18,560
Okay.
127
00:07:18,930 --> 00:07:23,130
And then we will call the function sum up to do with num equals due to a right.
128
00:07:23,160 --> 00:07:27,600
Because we want to return the result for this function and we don't know it yet.
129
00:07:28,050 --> 00:07:35,690
So the result of these function will be two plus sum up to sum up to one.
130
00:07:35,830 --> 00:07:36,100
OK.
131
00:07:36,360 --> 00:07:42,240
And also we are going to call this function when NUM equals two one in Iraq, aggressive manner.
132
00:07:42,480 --> 00:07:48,960
And in this case, we will see that the result of these functions sum up to one num equals two one.
133
00:07:49,230 --> 00:07:54,750
And we know that here, if we take a look at our stopping condition, if NUM equals to one, the function
134
00:07:54,750 --> 00:07:56,610
is just going to return one.
135
00:07:57,090 --> 00:08:00,900
So this function for sum up to one will return one.
136
00:08:00,930 --> 00:08:04,470
So instead of this one, we are going kind of backwards, right?
137
00:08:05,130 --> 00:08:08,970
Right now we are going instead of these function, we are going.
138
00:08:08,990 --> 00:08:11,970
Because we found the solution, we are going to specify one.
139
00:08:12,390 --> 00:08:16,590
So for sum up to two, we know the result will be three.
140
00:08:16,620 --> 00:08:23,610
So instead of these two plus one, we are going to specify here three and four, some up to three,
141
00:08:23,610 --> 00:08:24,690
which is this case.
142
00:08:24,990 --> 00:08:26,880
We are going to return six.
143
00:08:26,970 --> 00:08:29,280
And this result is going to be returned.
144
00:08:29,760 --> 00:08:33,870
Hearings that have this line instead of this are function call.
145
00:08:34,200 --> 00:08:36,120
There will be just six.
146
00:08:36,360 --> 00:08:38,720
So the result variable will hold.
147
00:08:38,750 --> 00:08:39,810
Right now, six.
148
00:08:40,120 --> 00:08:41,870
And let's actually see the read exact.
149
00:08:42,030 --> 00:08:45,330
That's what happened on build and run it.
150
00:08:45,600 --> 00:08:48,380
So three, we provided with the number three.
151
00:08:48,720 --> 00:08:50,700
And we see that the result is six.
152
00:08:51,030 --> 00:08:54,030
And if we want to test it out for five.
153
00:08:54,060 --> 00:08:57,390
And we expect for five the do there will be fifteen.
154
00:08:57,690 --> 00:09:00,850
And that's exactly what we can see on the council.
155
00:09:01,620 --> 00:09:05,460
So once again, just to go over this solution.
156
00:09:05,490 --> 00:09:10,740
This function solution, we wanted to write a function that will some of the numbers from one up to
157
00:09:10,740 --> 00:09:11,520
given NUM.
158
00:09:12,030 --> 00:09:20,790
And we wanted to use a recursive methodology, let's say, let's call it like this, and we need a recursive
159
00:09:21,210 --> 00:09:29,730
function call that we'll be able to split a given problem to find the sum up to a given num to some
160
00:09:29,820 --> 00:09:31,650
smaller sub problems.
161
00:09:32,220 --> 00:09:40,470
And we know that some up to a given num is actually the num itself, some number plus of a sum up to
162
00:09:40,530 --> 00:09:42,030
num minus one.
163
00:09:42,090 --> 00:09:44,340
OK, so we know that's the recursive call.
164
00:09:44,400 --> 00:09:49,030
That's some rule, some guideline that we use in this recursive function.
165
00:09:49,230 --> 00:09:57,390
And also we've seen that if we want to stop some at some point the recursive calls, we need to have
166
00:09:57,450 --> 00:09:59,220
a in conditions some base.
167
00:09:59,310 --> 00:10:01,090
Case and in this case.
168
00:10:01,150 --> 00:10:08,140
We used a base cases Nahm equals to one, because we know that it's very trivial that some from one
169
00:10:08,140 --> 00:10:09,820
up to one is just one.
170
00:10:09,850 --> 00:10:12,460
There was no even math operations.
171
00:10:12,610 --> 00:10:14,800
So this is a four of these video.
172
00:10:14,800 --> 00:10:20,890
I hope that's clear how the function calls its how the function calls itself from within it.
173
00:10:21,160 --> 00:10:22,900
We just call another instance.
174
00:10:22,960 --> 00:10:29,860
Think of it as just on a different block units that are calling one another and expecting to receive
175
00:10:29,860 --> 00:10:31,900
a result from the next one.
176
00:10:32,320 --> 00:10:35,200
So thank you guys for watching and I'll see you in the next example.
15325
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.