All language subtitles for 8. [Dart] Functions Challenge

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
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 All right. 1

So now that you've learned about all three types of functions, it's time to challenge yourself with an 2

exercise. 3

And in this case, I've created a pre-built dartpad for you with some code that I've already written. 4

And you can access this exact same dartpad by heading over to this URL which I will of course linked 5

to in the course resources for this lesson. 6

Now once you get here, you can see that we have a challenge for you. 7

And the idea is that without changing this main function so without touching any of these lines of code 8

that are already written, 9

I want you to make this code work and get the result printed in the console. In order to achieve this, 10

you're going to need to create a add function and also a multiply function, because at the moment, this 11

is what our Dart analysis is telling us that doesn't exist. 12

So there's no add function and there's no multiply function. 13

So somewhere down here after the main function, create these two functions, add and multiply. 14

Making sure that you spell them exactly the same and also match up the input parameter names where there 15

are any. 16

Once you've completed the challenge, firstly all the errors should go away and it should now look like 17

this. 18

And when you click run, it should print the result which in this case should be 23.3 19

recurring into the console. The way that we're able to calculate this is because our add function adds 20

our two numbers, where we give it as inputs 5 and 9 together. 21

So 5 + 9 = 14. 22

And then that 14 becomes the result of this right hand side, 23

and it goes into the next step where we tried to multiply 14 by 5. 24

So there's multiply function has to multiply this value by this second value. 25

I've obviously hidden what the steps are in order to achieve this 26

so that you can test yourself and see if you can make this work. 27

So pause the video now and try to complete this challenge. 28

All right. 29

So how did you get on? 30

Well in order to complete this challenge, you need to have a good grasp of functions. 31

So the first thing we'll address here is that right here, we are creating a new variable that has a data 32

type of int, 33

so a whole number. And it's going to be storing something that comes out as the output of this function. 34

The function has to be called add, and it takes two inputs, n1 and n2, 35

and in this case we're saying 5 and 9. And inside this function obviously we need to add 5 to 9 36

in order for it to come out as our result. 37

So let's create our function now. 38

Previously we've always started with void. 39

In this case because we want to have an output, 40

and in this case we want to output a whole number, 41

we're going to start with the keyword int. And then we're going to provide the name of our function, which 42

is called add. 43

And you can see that once I put in that word, then my error for the add function goes away. This is the 44

bare bones of my function so far. But I now need to say that it provides some inputs. 45

And in this case because we're able to call add and provide our inputs with named parameters or named 46

arguments, we have to add in those curly braces in here so that we can write int n1 and int n2. 47

So what this does is it says that when you call the function add, you can provide the name of each parameter, 48

just like we have done here. And both inputs are going to be integers, 49

so we have the int keyword in there. 50

And then inside the function, we're simply going to add n1 to n2. So you can either create a variable 51

called result and you write equals n1 + n2, and then you can output the result by using that 52

return keyword. 53

So you write return result. And this will get rid of all our errors to do with our step 1. 54

Now if this makes more sense to you, then be sure to write it like this, but you can also cut this down 55

by one line. By instead of having to create that variable, 56

you can also just simply return n1 + n2. And we can remove this line entirely. 57

So that does exactly the same thing but it combined that into one step. And we don't have to create that 58

intermediate step that we had previously. 59

Now that add is done, we have to create our multiply function. 60

Let's go down here and create our multiply. 61

It's also going to have an output of integer type because we can see that the variable that we're using 62

to hold the data that comes out of this function, has the data type int. 63

So this must have an output of int. 64

So we're going to start off with int, and then we're going to give it a name. 65

So it's called multiply, making sure those are spelled the same. But in this case, do you see that we don't 66

have the argument names when we call the function? 67

So we didn't have to write n1: or n2:. 68

So that means we don't need those curly braces and we can simply just specify our data type and the name 69

of our argument, which will again call n1 and n2. And they are also going to be integers. 70

Now inside our multiply, we're going to of course return the result of multiplying n1 by n2. And 71

this will come out of the multiply function as the output. And that output goes into here. 72

So the output is gonna be equal to the step 2 result which then gets used in the next step of calculations 73

where it gets divided by 3 and finally that final result is printed. 74

So now when we hit run, we can see the final output is 23.3 recurring, and this is 75

how you would solve this challenge. 76

Now if any point of this challenge is confusing, 77

be sure to revisit the lessons where we talked about functions in Dart part 1, part 2 and Part 3. 78

But otherwise, we're going to get back to coding up our Xylophone app and trying to solve the problem 79

that we had earlier on using our newfound knowledge. 80

So all of that and more, 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.