Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,510 --> 00:00:01,890
What is going on, guys?
2
00:00:01,960 --> 00:00:08,190
And in this exercise, what we are going to do is to write a function that uses pointers and we are
3
00:00:08,190 --> 00:00:12,090
going to introduce the concept of passing by reference.
4
00:00:12,480 --> 00:00:14,910
So let's start with the simple exercise.
5
00:00:14,970 --> 00:00:16,890
And first of all, we are going to do it.
6
00:00:17,500 --> 00:00:21,240
What we are going to do is to write a function that receives through numbers.
7
00:00:21,270 --> 00:00:22,740
Number one and number two.
8
00:00:23,190 --> 00:00:27,060
And all these function does is to return the maximum between them.
9
00:00:27,390 --> 00:00:29,490
So these functions simply looks like this.
10
00:00:30,150 --> 00:00:32,710
You have the function name, which is find marks.
11
00:00:33,030 --> 00:00:38,610
The function is of an integer, a tie because it returns an end and it receives through numbers.
12
00:00:38,670 --> 00:00:39,360
Number one.
13
00:00:39,360 --> 00:00:40,050
And number two.
14
00:00:40,260 --> 00:00:44,610
And inside of the functions body, we have just one condition we check out.
15
00:00:44,640 --> 00:00:48,960
If no one is greater than number two and in this case, we return number one.
16
00:00:49,320 --> 00:00:51,260
Otherwise, we return number two.
17
00:00:51,300 --> 00:00:53,890
So simply find out the maximum.
18
00:00:53,910 --> 00:00:55,540
Between two given numbers.
19
00:00:55,820 --> 00:01:01,200
And now if we are to write the main section for this exercise, it will look something like this will
20
00:01:01,200 --> 00:01:03,060
create the void main function.
21
00:01:03,420 --> 00:01:09,660
We will initialize two variables in a in and in the B, initialize A to be equal to five.
22
00:01:09,660 --> 00:01:11,670
Bique will be equal to seven.
23
00:01:12,330 --> 00:01:16,200
And then we will create another additional variable called Marks.
24
00:01:16,330 --> 00:01:20,460
And we will call the function find Marks and Bassett by value.
25
00:01:20,470 --> 00:01:27,210
Just the copy, as we discussed previously, a copy of the Values A and B and the Find Marks function
26
00:01:27,210 --> 00:01:31,030
will find out of a maximum between five and seven.
27
00:01:31,200 --> 00:01:38,160
In this example and return are return these value and storeyed inside of marks variable.
28
00:01:38,190 --> 00:01:44,400
So here you can see mux equals to find marks and marks variable after the function will return.
29
00:01:44,460 --> 00:01:49,050
The maximum will hold the value of seven in this example.
30
00:01:49,230 --> 00:01:55,170
So it can try to write and run this program on your own and see what will happen when you use this print
31
00:01:55,210 --> 00:01:55,700
off-line.
32
00:01:55,750 --> 00:02:01,170
That will represent the maximum between five and seven will be equal to seven.
33
00:02:01,180 --> 00:02:03,000
So the percentage did the last one.
34
00:02:03,030 --> 00:02:06,990
The third one will be replaced with the marks value.
35
00:02:07,200 --> 00:02:07,680
All right.
36
00:02:07,710 --> 00:02:08,690
So I hope that's clear.
37
00:02:08,730 --> 00:02:10,290
Nothing complicated so far.
38
00:02:10,620 --> 00:02:16,200
Now, let's say that we wanted to write another function, that all it has to do is to find the minimum
39
00:02:16,200 --> 00:02:17,610
between two given numbers.
40
00:02:17,790 --> 00:02:19,500
So basically, it will look like this.
41
00:02:19,710 --> 00:02:25,080
We will have the name of the function, which is fine mean all the structure is pretty much the same
42
00:02:25,170 --> 00:02:29,670
as we used it in find marks here on the left, just the return value.
43
00:02:30,100 --> 00:02:34,140
The result of the condition is just a little bit different.
44
00:02:34,200 --> 00:02:39,900
So if no one is greater than, um, two, then as opposed to what we're using, find monks here.
45
00:02:39,900 --> 00:02:40,160
We will.
46
00:02:40,410 --> 00:02:43,950
We will return the minimum the number two in this example.
47
00:02:44,310 --> 00:02:46,190
And otherwise, we will return No.
48
00:02:46,240 --> 00:02:46,680
One.
49
00:02:46,860 --> 00:02:49,230
So very similar to the maximum function.
50
00:02:49,260 --> 00:02:55,140
The only thing that defers here is what are we will conclude out of this condition.
51
00:02:55,170 --> 00:03:00,150
So whether we will return the maximum or the minimum between two given numbers.
52
00:03:00,510 --> 00:03:06,120
Now, once we have both of these functions, we can update the void main function to also support the
53
00:03:06,120 --> 00:03:07,830
search for the minimum.
54
00:03:08,250 --> 00:03:15,420
So we just need to add another minimum variable and we can call both for both of these functions for
55
00:03:15,420 --> 00:03:21,930
fine marks and for find mean that once we got both of them, we have inside of these marks variable
56
00:03:21,930 --> 00:03:22,830
and mean variable.
57
00:03:23,160 --> 00:03:30,030
We have the associated values, which in this case it will be just that the minimum holds five and the
58
00:03:30,030 --> 00:03:31,550
maximum holds seven.
59
00:03:31,800 --> 00:03:37,140
And of course, you can use print F lines to to print the results or the result that will be printed
60
00:03:37,680 --> 00:03:44,640
on the screen here is maximum between five and seven equals to seven and minimum between five and seven
61
00:03:44,730 --> 00:03:46,230
equals to five.
62
00:03:46,800 --> 00:03:53,100
Now what I want us to do is instead of writing these two separate functions, find mean and fine marks.
63
00:03:53,250 --> 00:03:56,250
I want us to unify them into just one function.
64
00:03:56,300 --> 00:04:00,120
That will be able to find both the maximum and the minimum.
65
00:04:00,720 --> 00:04:03,240
But the main question here is how we can do it.
66
00:04:03,540 --> 00:04:09,880
Let's say we just created our unified function and we called it like fine min max.
67
00:04:10,320 --> 00:04:12,470
And this function basically will look like this.
68
00:04:12,690 --> 00:04:16,650
It will have the function name find min max and equal will received.
69
00:04:16,650 --> 00:04:23,130
Two numbers will create also two additional variables, Min and Max to hold the minimum and the maximum.
70
00:04:23,400 --> 00:04:27,990
And then we will use some condition to see if no one is greater than two.
71
00:04:28,410 --> 00:04:33,630
Then we know that in this case, minimum equals two, number two and maximum equals two num one.
72
00:04:33,900 --> 00:04:37,440
Otherwise minimum equals to number one and maximum equals two.
73
00:04:37,440 --> 00:04:37,960
Number two.
74
00:04:38,280 --> 00:04:44,280
Nothing complicated so far in once we found out what is the minimum and the maximum, we will simply
75
00:04:44,370 --> 00:04:47,020
return minimum and return maximum.
76
00:04:47,580 --> 00:04:50,250
And at first glance, everything seems to be great.
77
00:04:50,280 --> 00:04:56,070
It seems they add the function, finds out the minimum and the maximum and returns both of them, just
78
00:04:56,070 --> 00:04:56,790
like we wanted.
79
00:04:57,270 --> 00:04:59,500
But the problem here is that we can not.
80
00:05:00,350 --> 00:05:03,020
More than one thing in C language.
81
00:05:03,290 --> 00:05:07,310
You remember we've said that a function may return only up to one value.
82
00:05:07,340 --> 00:05:12,320
And here we actually try to return to things the minimum and the maximum.
83
00:05:12,710 --> 00:05:15,540
And that's basically something we can not do in C.
84
00:05:16,210 --> 00:05:18,590
Well, maybe some other programming languages.
85
00:05:18,890 --> 00:05:22,730
For example, in Python, you can do it without any problem in C.
86
00:05:22,790 --> 00:05:23,840
That is not the case.
87
00:05:23,930 --> 00:05:24,710
You can do it.
88
00:05:24,980 --> 00:05:26,240
But it's still possible.
89
00:05:26,270 --> 00:05:30,500
Basically lets us talk about and explain exactly what we want to do.
90
00:05:30,980 --> 00:05:32,570
What we want is simply one.
91
00:05:32,570 --> 00:05:39,290
We call this function when we call it just once, both of the variables meaning moment maximum in the
92
00:05:39,290 --> 00:05:40,370
main function, OK?
93
00:05:40,430 --> 00:05:46,040
They will receive their associated values based on the result of the minimum and maximum condition,
94
00:05:46,040 --> 00:05:46,520
of course.
95
00:05:46,730 --> 00:05:48,260
So think of it this way.
96
00:05:48,320 --> 00:05:53,120
Are you having the main function, let's say A and B, initialize two, five and seven like we have
97
00:05:53,120 --> 00:05:53,540
here?
98
00:05:53,930 --> 00:05:56,690
And also, you have two variables, minimum and maximum.
99
00:05:57,200 --> 00:06:03,620
And once you call this special function that we are going to develop right now, then both of the variables,
100
00:06:03,620 --> 00:06:07,580
minimum and maximum, which previously were not even initialized.
101
00:06:07,730 --> 00:06:10,910
Both of them will contain the value that they should have.
102
00:06:10,940 --> 00:06:14,960
So in this case, minimum will contain five and maximum will contain seven.
103
00:06:15,140 --> 00:06:21,590
And we can undo it with a simple return because we've seen that we cannot return more than just one
104
00:06:21,590 --> 00:06:22,250
value.
105
00:06:22,610 --> 00:06:28,790
So that's exactly why we should start thinking out of another way to solve it, probably using pointers
106
00:06:29,270 --> 00:06:36,050
which will allow us to modify by reference the values of Min and max variables in the main function.
107
00:06:36,350 --> 00:06:42,350
So we are going to modify the values themselves in the main function from another function.
108
00:06:42,380 --> 00:06:44,700
Let's say we will call it find min max.
109
00:06:44,960 --> 00:06:51,130
We are going to modify the values are in the main function from another function.
110
00:06:51,200 --> 00:06:55,430
And that's actually called to to modify by reference.
111
00:06:55,460 --> 00:06:56,270
So room.
112
00:06:56,390 --> 00:06:57,320
Remember this one?
113
00:06:57,320 --> 00:06:58,400
It's very important.
114
00:06:58,580 --> 00:07:05,180
And the way we'll do it is simply by sending the addresses of minimum and maximum variables from the
115
00:07:05,690 --> 00:07:06,950
from the main function.
116
00:07:07,310 --> 00:07:09,650
And then in the fine min max function.
117
00:07:09,680 --> 00:07:09,910
OK.
118
00:07:10,160 --> 00:07:11,570
This is our second function.
119
00:07:11,990 --> 00:07:18,170
We will get these addresses and point to them using pointers, just like we've seen previously.
120
00:07:18,380 --> 00:07:20,210
So basically it will look like this.
121
00:07:20,240 --> 00:07:20,530
OK.
122
00:07:20,810 --> 00:07:25,400
We will send the addresses of marks and men and these addresses will be stored.
123
00:07:25,460 --> 00:07:25,650
OK.
124
00:07:25,730 --> 00:07:27,950
Will be received in the function.
125
00:07:27,980 --> 00:07:28,280
OK.
126
00:07:28,340 --> 00:07:32,420
Suppose that you have your function called find mean marks on the right.
127
00:07:32,810 --> 00:07:34,450
And it has two parameters.
128
00:07:34,470 --> 00:07:38,540
B mean pointer to mean and B marks which is a pointer to marks.
129
00:07:38,720 --> 00:07:45,800
And these two variables, B mean and B Marks has of the addresses of the variables, minimum and maximum
130
00:07:45,800 --> 00:07:46,520
accordingly.
131
00:07:46,940 --> 00:07:51,380
So what we are going to do then is simply to write a function.
132
00:07:51,410 --> 00:07:53,690
This function is going to be a for void type.
133
00:07:53,870 --> 00:07:57,230
And what what we are going to do now is simply too.
134
00:07:57,380 --> 00:08:03,200
We can see that this function will receive two value, two two values into number one and number two,
135
00:08:03,200 --> 00:08:10,010
which contains, let's say, five and seven like we had previously, A and B, initialize to five and
136
00:08:10,010 --> 00:08:10,460
seven.
137
00:08:11,120 --> 00:08:18,050
So the first two parameters are just the values and the next two parameters, which are P marks and
138
00:08:18,050 --> 00:08:20,630
B mean they are of a pointer type.
139
00:08:20,640 --> 00:08:20,870
Right.
140
00:08:20,930 --> 00:08:23,120
You can see that both of them are a point.
141
00:08:23,120 --> 00:08:24,350
There is two integers.
142
00:08:24,620 --> 00:08:29,410
So at this point, you know that you sent through this third and fourth variables.
143
00:08:29,870 --> 00:08:38,390
Let's say that the fourth and the third and fourth parameters, B marks and B mean know how to access
144
00:08:38,480 --> 00:08:39,360
the variables.
145
00:08:39,450 --> 00:08:41,930
Meaning marks on the main function.
146
00:08:42,110 --> 00:08:44,540
And now the structure is going to be the same.
147
00:08:44,630 --> 00:08:50,750
These function basically does not return anything because that's why you will not see a return statement.
148
00:08:51,170 --> 00:08:55,520
And what you are going to do is to use a simple condition like we've done previously.
149
00:08:55,520 --> 00:08:57,870
So if not, one is greater than I am two.
150
00:08:58,250 --> 00:09:03,320
And now, instead of modifying it locally to the find mean max function.
151
00:09:03,710 --> 00:09:11,570
We know that by using points or B mean we can access the actual value, the actual variable that is
152
00:09:11,570 --> 00:09:17,900
called mean in the main function and modify it, modify its value to be equal to number two.
153
00:09:18,260 --> 00:09:25,190
And also the same for P marks points are P marks equals two on one that basically will update in will
154
00:09:25,190 --> 00:09:30,770
modify the value of the variable marks on the mean in the main function.
155
00:09:31,070 --> 00:09:32,270
So I hope that's clear.
156
00:09:32,360 --> 00:09:35,880
It's super important, guys, to understand this concept.
157
00:09:36,000 --> 00:09:38,330
How to you pass by reference.
158
00:09:38,720 --> 00:09:45,290
So simply, once again, what we've done here was simply sent the addresses of the variables that we
159
00:09:45,290 --> 00:09:47,990
wanted them to be modified by reference.
160
00:09:48,020 --> 00:09:48,210
OK.
161
00:09:48,260 --> 00:09:56,030
Behind the scenes, once we call this function find mean marks, they will be modified without even,
162
00:09:56,300 --> 00:09:57,410
let's say, noticing it.
163
00:09:57,470 --> 00:09:57,740
OK.
164
00:09:58,010 --> 00:09:59,480
So just to make it more clear.
165
00:09:59,600 --> 00:09:59,740
Let.
166
00:09:59,930 --> 00:10:01,240
Tried to use to.
167
00:10:01,490 --> 00:10:04,100
I show you the void main function.
168
00:10:04,400 --> 00:10:10,010
So we initially stole variables five and seven into equals to five equals to seven.
169
00:10:10,520 --> 00:10:15,290
And we also create would be clear to another variables, Max and Min.
170
00:10:15,530 --> 00:10:19,130
And at this point, meaning marks do not contain anything at all.
171
00:10:19,400 --> 00:10:21,590
They simply just variables.
172
00:10:22,190 --> 00:10:25,400
Then we are going to call the find min max function.
173
00:10:25,550 --> 00:10:25,930
Okay.
174
00:10:25,970 --> 00:10:30,010
And the two first values that we pass is just by value.
175
00:10:30,020 --> 00:10:31,580
We just sent five and seven.
176
00:10:31,910 --> 00:10:37,810
And these five and seven from the main function are going to be two to be stored to be best to know
177
00:10:37,820 --> 00:10:40,130
one and two in the find min max function.
178
00:10:40,310 --> 00:10:40,820
That's it.
179
00:10:41,000 --> 00:10:47,840
And then the third and the fourth values that we are going to send are basically the addresses of these
180
00:10:47,840 --> 00:10:48,590
variables.
181
00:10:48,620 --> 00:10:48,860
Right.
182
00:10:48,890 --> 00:10:50,000
We have the ampersand.
183
00:10:50,360 --> 00:10:55,550
So we send the address of variable marks and of the address of variable mean.
184
00:10:56,030 --> 00:10:59,000
And who catches these addresses.
185
00:10:59,120 --> 00:11:06,350
Simply P. marks and be mean, which will point to the variables marks and mean here in the main function.
186
00:11:06,680 --> 00:11:12,920
And then once this function is over, once, once these third line in the void, main function is complete.
187
00:11:13,250 --> 00:11:15,080
Europe can print of the values.
188
00:11:15,140 --> 00:11:20,210
So you can print the maximum and the minimum in both of these variables will be updated.
189
00:11:20,420 --> 00:11:23,180
Behind the scenes by reference.
190
00:11:23,330 --> 00:11:24,860
So I hope that's clear to you.
191
00:11:24,890 --> 00:11:29,210
And what is the difference between passing by value and passing by reference?
192
00:11:29,360 --> 00:11:35,570
It's super crucial to for for you to understand it right now, because in the more advanced courses,
193
00:11:35,940 --> 00:11:42,620
this this whole structure of working with by reference, let's say, is very important.
194
00:11:42,650 --> 00:11:43,940
You will see it a lot.
195
00:11:44,270 --> 00:11:50,390
And it's actually the core for much more for a lot of courses in a lot of fields.
196
00:11:50,690 --> 00:11:54,140
So you better know it and you better practice it a little bit.
197
00:11:54,170 --> 00:11:55,010
I will leave you.
198
00:11:56,030 --> 00:11:57,210
Additional exercise.
199
00:11:57,440 --> 00:12:01,400
Probably a milestone, too, to practice this topic.
200
00:12:01,550 --> 00:12:04,790
And basically, I can say that this is it for this video.
201
00:12:04,850 --> 00:12:06,860
Let me know if you have any questions.
202
00:12:07,220 --> 00:12:09,830
And definitely take a break after this lesson.
203
00:12:09,920 --> 00:12:14,960
And maybe when you're Frasch go over the material once again or twice again.
204
00:12:15,170 --> 00:12:16,880
So thank you guys for watching.
205
00:12:16,910 --> 00:12:18,850
And I wish you a great day.
19113
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.