All language subtitles for 121 Functions Part 3_ Outputs _ Return Values.en_US

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic Download
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
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

Now in the last lesson we looked at the chocolate flavor of functions, which are functions that are able

to take an input and use that input inside the function to do some calculation or to use it as a part

of the code.

Now in this lesson, I want to show you an even more complex and also more useful type of function, which

are functions that are able to give an output.

So for example, taking the previous example where we're able to take money as an input to the function

and use it to calculate the number of bottles of milk that we can buy, at the end of the code we can

use the keyword return to specify that this function, in addition to being able to take inputs,

it can also give an output.

And in our case we're giving the output of money modulo 1.5.

So remember that 1.5 is the cost of a bottle of milk.

So money modulo 1.5 means that we're getting the remainder of how much we've spent to buy the milk.

So, if you remember, the modulo works like this. Whereas 9 divide by 6, 6 only goes into 9 once.

So we get a remainder of three.

So in this case the variable e is equal to 3, because the modulo gives you the remainder of the division.

So in this function if we gave an input of $4 and each bottle of milk cost $1.5 then the robot will

buy 2 bottles of milk which will cost $3 and it'll give us $1 change. And that $1 of

change is the output from this function which we can bind to a variable called change.

So, for example, we can say variable change is equal to the outcome of the function

getMilk with an input of 4.

And change will equal 1, because remember each bottle of milk costs 1.5.

So $4 only gets you two bottles of milk which costs $3 and that gives you a remainder of 1.

So now the output of this entire function is equal to 1,

and that gets assigned as the value to the variable change. That might take a little bit to sit in.

But let's just review creating the function, this third type of function.

So the important things are we have the function keyword that specifies we're creating a new function.

We have to give the function a name straight afterwards and then inside the curly braces we specify

whether or not if we have an input that this function is going to use.

And in order to get an output out of the function we have to use that keyword return, and whatever gets

returned in the function becomes the right hand side of this function call.

So when we call a function that has an output we can use the output and we can assign it to a variable,

for example in this case the variable change.

So if we didn't have this line where we have a return statement or specify that our function has an output,

then calling getMilk only executes the function without getting an output back.

So this is the most complex of all three functions.

And this is the last flavor that we're looking at, which is the strawberry flavor, which incidentally

is also the flavor that I never touched because it tastes awful.

But our third function doesn't have to be awful as long as we understand how the return works.

So let's take a look at this in practice.

So currently we've got a function that takes an input called money, and it uses that input to calculate

how many bottles of milk we're able to buy using that money.

But at the end of the function, just before we hit the closing brace, we can add a return keyword and

specify that we want this function to output a number and the number is going to be money modulo 1.5

which is the cost of a bottle of milk.

So this will give us the remainder of this division.

So that means that if we give say an input of 3, 3 divided by 1.5 has a remainder of 0.

So we'll end up returning 0. But if we gave it a value 4, then we will have a remainder of 1, and that

number 1 becomes the output of this function call.

So we can capture it and save it inside a variable called change, for example, and I can log the value

of change by writing console.log(change).

So now if I hit run you can see that all of those lines of code get executed and it shows me that I'm

buying two bottles of milk but it also shows me that I'm getting a change of one and that comes from

this return statement.

So this is quite useful because it allows us to use a number of functions inside other functions.

So for example if, instead of messing up my bunch of instructions, I took this part where I calculate

the number of bottles of milk I get based on money into its own function.

So, for example, let’s create a function called calcBottles, and it takes an input in the form of startingMoney

and also costPerBottle.

So we can now say that the number of bottles is equal to this input called startingMoney divided by

the costPerBottle,

right?

This is always going to be the same calculation that you need to perform no matter which scenario.

So you could have a starting money amount of $5 $10 $20.

It doesn't matter.

And if you divide that by the cost per bottle of milk then you will end up with the number of bottles.

So now, how can we use this function inside here?

Well, if we give this function a return statement, and we say that the output of this function is the

number of bottles, then up here we can write console.log “buy “ + calculate bottles using the amount

of money we got as an input over here as the first parameter, which is startingMoney,

and the second input, 1.5 as the second parameter, which is the cost per bottle, and now, once our code

gets to this line, which is line number 12, then it’ll go and find where this function was created, calcBottles,

which is down here of course,

it will place the value of money into startingMoney, 1.5 into costPerBottle, and it will calculate

the number of bottles.

And once it gets to this return statement, that value in numberOfBottles will replace this part of

the code because it is the output of the function.

So now if I call my first function getMilk and I specify an input of say $5, then that $5 is going to

go into this parameter called money and then it's going to move over here in order to use it to calculate

the number of bottles we’ll get back.

So if I hit run you'll see that all of the instructions are executed and it also tells me that I'm able

to buy 4 bottles of milk and have 0.5 as the change.

Now what if we did the same thing down here?

What if, instead of doing the calculation in here, we created another function, and we called this function

calcChange, and the inputs that it's going to take is startingAmount and also costPerBottle.

And inside this function I'm going to calculate change, which is going to be equal to startingAmount

modulo costPerBottle.

And that is what I'm going to return as the output of this function.

So now, instead of returning this calculation, I can simply return calcChange, and the first input is

going to be money,

and the second input, costPerBottle, is going to be 1.5, and that becomes the output of this function,

which is getMilk.

So instead of just calling getMilk here, I can also say console.log, call this function getMilk,

and we're going to add it to a string,

so we're going to say “Hello master,

here is your “

whichever amount that you get + “ change.”.

So now if I run the code you can see that it goes through all of the commands, buys 3 bottles of

milk,

and finally we get “Hello master, here is your $0.5 of change.”.

So as you can see what we're doing here is we're using the outputs of functions in order to use it inside

other lines of code.

So now, say if the cost per bottle of milk changed,

right?

Say it now costs $2.5 instead of 1.5.

Then we don't have to change the calculation.

And in fact we can make this even more dry by reducing the repetition inside the function.

So as an input we can say getMilk, the amount of money as an input and also the cost per bottle as an

input,

and we can replace that hit.

So now, when we call getMilk, we have to say the amount of money we got given is 5 and the cost per

bottle is say 1.5.

So this will give us 3 bottles of milk and 0.5 change, but say tomorrow the master gives

us $10 to buy milk,

and the cost of milk has gone up to $3, and now we're able to only buy 3 bottles of milk but we get

$1 change.

You will find yourself using all three types of functions in a variety of scenarios depending on what

it is that you're trying to do.

There's a lot of new information that you've learned.

So it's really really crucial that you try to understand this information by completing some exercises

and trying to write the code yourself.

So in the next lesson I have an interactive challenge for you where I want you to write some code and

we'll use some software on our site to test your code and see if you got it right.

So for all of that and more exciting stuff, I'll see you on the next lesson.

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