All language subtitles for 25. Functions Part 3 Outputs & Return Values

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 Download
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

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

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

of the code. 3

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

are functions that are able to give an output. 5

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

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

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

it can also give an output. 9

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

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

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

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

So we get a remainder of three. 14

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

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

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

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

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

getMilk with an input of 4. 20

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

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

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

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

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

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

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

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

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

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

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

for example in this case the variable change. 32

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

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

So this is the most complex of all three functions. 35

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

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

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

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

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

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

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

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

which is the cost of a bottle of milk. 44

So this will give us the remainder of this division. 45

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

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 47

number 1 becomes the output of this function call. 48

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

of change by writing console.log(change). 50

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 51

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

this return statement. 53

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

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

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

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

and also costPerBottle. 58

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

the costPerBottle, 60

right? 61

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

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

It doesn't matter. 64

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

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

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

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

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

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

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

which is down here of course, 72

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

the number of bottles. 74

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

the code because it is the output of the function. 76

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

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

the number of bottles we’ll get back. 79

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

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

Now what if we did the same thing down here? 82

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

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

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

modulo costPerBottle. 86

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

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

going to be money, 89

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

which is getMilk. 91

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

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

so we're going to say “Hello master, 94

here is your “ 95

whichever amount that you get + “ change.”. 96

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

milk, 98

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

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

other lines of code. 101

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

right? 103

Say it now costs $2.5 instead of 1.5. 104

Then we don't have to change the calculation. 105

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

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

input, 108

and we can replace that hit. 109

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

bottle is say 1.5. 111

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

us $10 to buy milk, 113

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 114

$1 change. 115

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

it is that you're trying to do. 117

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

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

and trying to write the code yourself. 120

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

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

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.