All language subtitles for 7. Pass by Reference + FULL EXAMPLE

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian Download
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

What is going on, guys?

And in this exercise, what we are going to do is to write a function that uses pointers and we are

going to introduce the concept of passing by reference.

So let's start with the simple exercise.

And first of all, we are going to do it.

What we are going to do is to write a function that receives through numbers.

Number one and number two.

And all these function does is to return the maximum between them.

So these functions simply looks like this.

You have the function name, which is find marks.

The function is of an integer, a tie because it returns an end and it receives through numbers.

Number one.

And number two.

And inside of the functions body, we have just one condition we check out.

If no one is greater than number two and in this case, we return number one.

Otherwise, we return number two.

So simply find out the maximum.

Between two given numbers.

And now if we are to write the main section for this exercise, it will look something like this will

create the void main function.

We will initialize two variables in a in and in the B, initialize A to be equal to five.

Bique will be equal to seven.

And then we will create another additional variable called Marks.

And we will call the function find Marks and Bassett by value.

Just the copy, as we discussed previously, a copy of the Values A and B and the Find Marks function

will find out of a maximum between five and seven.

In this example and return are return these value and storeyed inside of marks variable.

So here you can see mux equals to find marks and marks variable after the function will return.

The maximum will hold the value of seven in this example.

So it can try to write and run this program on your own and see what will happen when you use this print

off-line.

That will represent the maximum between five and seven will be equal to seven.

So the percentage did the last one.

The third one will be replaced with the marks value.

All right.

So I hope that's clear.

Nothing complicated so far.

Now, let's say that we wanted to write another function, that all it has to do is to find the minimum

between two given numbers.

So basically, it will look like this.

We will have the name of the function, which is fine mean all the structure is pretty much the same

as we used it in find marks here on the left, just the return value.

The result of the condition is just a little bit different.

So if no one is greater than, um, two, then as opposed to what we're using, find monks here.

We will.

We will return the minimum the number two in this example.

And otherwise, we will return No.

One.

So very similar to the maximum function.

The only thing that defers here is what are we will conclude out of this condition.

So whether we will return the maximum or the minimum between two given numbers.

Now, once we have both of these functions, we can update the void main function to also support the

search for the minimum.

So we just need to add another minimum variable and we can call both for both of these functions for

fine marks and for find mean that once we got both of them, we have inside of these marks variable

and mean variable.

We have the associated values, which in this case it will be just that the minimum holds five and the

maximum holds seven.

And of course, you can use print F lines to to print the results or the result that will be printed

on the screen here is maximum between five and seven equals to seven and minimum between five and seven

equals to five.

Now what I want us to do is instead of writing these two separate functions, find mean and fine marks.

I want us to unify them into just one function.

That will be able to find both the maximum and the minimum.

But the main question here is how we can do it.

Let's say we just created our unified function and we called it like fine min max.

And this function basically will look like this.

It will have the function name find min max and equal will received.

Two numbers will create also two additional variables, Min and Max to hold the minimum and the maximum.

And then we will use some condition to see if no one is greater than two.

Then we know that in this case, minimum equals two, number two and maximum equals two num one.

Otherwise minimum equals to number one and maximum equals two.

Number two.

Nothing complicated so far in once we found out what is the minimum and the maximum, we will simply

return minimum and return maximum.

And at first glance, everything seems to be great.

It seems they add the function, finds out the minimum and the maximum and returns both of them, just

like we wanted.

But the problem here is that we can not.

More than one thing in C language.

You remember we've said that a function may return only up to one value.

And here we actually try to return to things the minimum and the maximum.

And that's basically something we can not do in C.

Well, maybe some other programming languages.

For example, in Python, you can do it without any problem in C.

That is not the case.

You can do it.

But it's still possible.

Basically lets us talk about and explain exactly what we want to do.

What we want is simply one.

We call this function when we call it just once, both of the variables meaning moment maximum in the

main function, OK?

They will receive their associated values based on the result of the minimum and maximum condition,

of course.

So think of it this way.

Are you having the main function, let's say A and B, initialize two, five and seven like we have

here?

And also, you have two variables, minimum and maximum.

And once you call this special function that we are going to develop right now, then both of the variables,

minimum and maximum, which previously were not even initialized.

Both of them will contain the value that they should have.

So in this case, minimum will contain five and maximum will contain seven.

And we can undo it with a simple return because we've seen that we cannot return more than just one

value.

So that's exactly why we should start thinking out of another way to solve it, probably using pointers

which will allow us to modify by reference the values of Min and max variables in the main function.

So we are going to modify the values themselves in the main function from another function.

Let's say we will call it find min max.

We are going to modify the values are in the main function from another function.

And that's actually called to to modify by reference.

So room.

Remember this one?

It's very important.

And the way we'll do it is simply by sending the addresses of minimum and maximum variables from the

from the main function.

And then in the fine min max function.

OK.

This is our second function.

We will get these addresses and point to them using pointers, just like we've seen previously.

So basically it will look like this.

OK.

We will send the addresses of marks and men and these addresses will be stored.

OK.

Will be received in the function.

OK.

Suppose that you have your function called find mean marks on the right.

And it has two parameters.

B mean pointer to mean and B marks which is a pointer to marks.

And these two variables, B mean and B Marks has of the addresses of the variables, minimum and maximum

accordingly.

So what we are going to do then is simply to write a function.

This function is going to be a for void type.

And what what we are going to do now is simply too.

We can see that this function will receive two value, two two values into number one and number two,

which contains, let's say, five and seven like we had previously, A and B, initialize to five and

seven.

So the first two parameters are just the values and the next two parameters, which are P marks and

B mean they are of a pointer type.

Right.

You can see that both of them are a point.

There is two integers.

So at this point, you know that you sent through this third and fourth variables.

Let's say that the fourth and the third and fourth parameters, B marks and B mean know how to access

the variables.

Meaning marks on the main function.

And now the structure is going to be the same.

These function basically does not return anything because that's why you will not see a return statement.

And what you are going to do is to use a simple condition like we've done previously.

So if not, one is greater than I am two.

And now, instead of modifying it locally to the find mean max function.

We know that by using points or B mean we can access the actual value, the actual variable that is

called mean in the main function and modify it, modify its value to be equal to number two.

And also the same for P marks points are P marks equals two on one that basically will update in will

modify the value of the variable marks on the mean in the main function.

So I hope that's clear.

It's super important, guys, to understand this concept.

How to you pass by reference.

So simply, once again, what we've done here was simply sent the addresses of the variables that we

wanted them to be modified by reference.

OK.

Behind the scenes, once we call this function find mean marks, they will be modified without even,

let's say, noticing it.

OK.

So just to make it more clear.

Let.

Tried to use to.

I show you the void main function.

So we initially stole variables five and seven into equals to five equals to seven.

And we also create would be clear to another variables, Max and Min.

And at this point, meaning marks do not contain anything at all.

They simply just variables.

Then we are going to call the find min max function.

Okay.

And the two first values that we pass is just by value.

We just sent five and seven.

And these five and seven from the main function are going to be two to be stored to be best to know

one and two in the find min max function.

That's it.

And then the third and the fourth values that we are going to send are basically the addresses of these

variables.

Right.

We have the ampersand.

So we send the address of variable marks and of the address of variable mean.

And who catches these addresses.

Simply P. marks and be mean, which will point to the variables marks and mean here in the main function.

And then once this function is over, once, once these third line in the void, main function is complete.

Europe can print of the values.

So you can print the maximum and the minimum in both of these variables will be updated.

Behind the scenes by reference.

So I hope that's clear to you.

And what is the difference between passing by value and passing by reference?

It's super crucial to for for you to understand it right now, because in the more advanced courses,

this this whole structure of working with by reference, let's say, is very important.

You will see it a lot.

And it's actually the core for much more for a lot of courses in a lot of fields.

So you better know it and you better practice it a little bit.

I will leave you.

Additional exercise.

Probably a milestone, too, to practice this topic.

And basically, I can say that this is it for this video.

Let me know if you have any questions.

And definitely take a break after this lesson.

And maybe when you're Frasch go over the material once again or twice again.

So thank you guys for watching.

And I wish you a great day.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.