All language subtitles for 7. [Dart] Functions - Part 3

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
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
tl Filipino
fi Finnish
fr French Download
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 0 1 00:00:00,760 --> 00:00:07,170 So in the last lesson, we came across this error here where it tells us that this expression here, our built 1 2 00:00:07,180 --> 00:00:12,960 Key function, has a type of void and therefore cannot be used here. 2 3 00:00:12,970 --> 00:00:19,520 So in order to solve this conundrum, we have to understand a little bit more about how functions work. 3 4 00:00:19,650 --> 00:00:22,590 So here I've deleted everything in my dartpad 4 5 00:00:22,680 --> 00:00:28,620 other than the main function. And I'm going to create a new function. I'm going to call it myFunction, 5 6 00:00:29,310 --> 00:00:35,940 and it's not going to have any input, so I'm going to leave the parentheses here empty, but it will do 6 7 00:00:35,940 --> 00:00:43,170 something. In between the curly braces, I'm going to create a new variable that's going to hold values 7 8 00:00:43,200 --> 00:00:49,740 of the type double, and it's gonna be called pi. So pi is gonna be its name. And I'm gonna set it to equal 8 9 00:00:49,740 --> 00:00:59,190 3.14159, the value of the mathematical pi. And in my function, all that's gonna happen is 9 10 00:00:59,190 --> 00:01:03,510 this value pi, it's gonna be multiplied by two. 10 11 00:01:03,780 --> 00:01:10,740 Now I've got my function created, we know that the main function is the entry point of all of our Dart 11 12 00:01:10,740 --> 00:01:11,080 files. 12 13 00:01:11,100 --> 00:01:16,870 So whenever we hit run, it's going to look inside here to see what should happen. Inside here, 13 14 00:01:16,890 --> 00:01:22,380 I'm going to call that function I just created, which is called myFunction, and it's gonna have some 14 15 00:01:22,440 --> 00:01:25,150 empty parentheses because it doesn't take inputs. 15 16 00:01:25,290 --> 00:01:30,740 And now if I click run, you can see that pretty much nothing happens. 16 17 00:01:30,740 --> 00:01:36,300 Now firstly because I'm not really printing anything to the console, and all of this just happens behind 17 18 00:01:36,300 --> 00:01:39,580 the scenes without us being able to see what happens. 18 19 00:01:40,380 --> 00:01:47,160 But rest assured, this does get called and this in fact, does get carried out and we can make sure of this 19 20 00:01:47,160 --> 00:01:49,840 by simply adding a print statement here. 20 21 00:01:49,860 --> 00:01:59,460 Let's print and say that, 'my function got called'. We know that if this gets printed into the console, then 21 22 00:01:59,550 --> 00:02:03,780 this must have been triggered successfully from our main function. 22 23 00:02:03,780 --> 00:02:05,130 And indeed it does. 23 24 00:02:05,130 --> 00:02:10,410 So we can be sure that these instructions do in fact get carried out. 24 25 00:02:10,410 --> 00:02:17,130 But how can we get hold of this value, this pi * 2? Kind of like the result of this function, 25 26 00:02:17,130 --> 00:02:17,370 right? 26 27 00:02:17,370 --> 00:02:21,390 How can we get hold of it and bring it into our main function? 27 28 00:02:21,420 --> 00:02:28,650 What if we wanted to get something out of this function so that once we run this function, it outputs 28 29 00:02:28,680 --> 00:02:36,330 the value that it's calculated and then we can store it inside a variable? Let's call it maybe result. 29 30 00:02:36,810 --> 00:02:42,300 And then we could maybe just print the result from our main function. 30 31 00:02:42,300 --> 00:02:47,280 So in this case, we're basically trying to do something inside my function. 31 32 00:02:47,280 --> 00:02:53,940 And then once the results been calculated, we want this to be transported back into our main function 32 33 00:02:54,210 --> 00:02:58,340 so that we can use it here to do other things such as printing it. 33 34 00:02:58,350 --> 00:03:00,500 Now at the moment, this doesn't work. 34 35 00:03:00,600 --> 00:03:07,110 And when I click run, you can see that the error that I get, both from the Dart error checker as well 35 36 00:03:07,110 --> 00:03:08,490 as in the console, 36 37 00:03:08,490 --> 00:03:16,330 it tells me that the expression here has a type of void and therefore can't be used. 37 38 00:03:16,350 --> 00:03:18,340 And it's pointing to this part, 38 39 00:03:18,350 --> 00:03:19,890 myFunction. 39 40 00:03:20,110 --> 00:03:25,060 Now if this sounds familiar, it's because it's the same error that we got over here. 40 41 00:03:25,480 --> 00:03:27,100 So what does this mean? 41 42 00:03:27,100 --> 00:03:35,830 Well it means that this function currently doesn't actually output anything. It outputs void, which in 42 43 00:03:35,830 --> 00:03:39,750 programming lingo, basically means that there is no output. 43 44 00:03:39,760 --> 00:03:43,490 Now what if we wanted it to give us a output? 44 45 00:03:43,540 --> 00:03:48,430 Well we would change that from void to the data type of the output. 45 46 00:03:48,460 --> 00:03:52,710 So let's say in this case, we times pi by 2. 46 47 00:03:52,720 --> 00:03:56,130 It's still going to be a number with decimal places right? 47 48 00:03:56,290 --> 00:04:00,270 So the data type of the output is still going to be a double. 48 49 00:04:00,850 --> 00:04:06,670 And the final thing we need to do, is we need to mark the thing that we want to come out of the function 49 50 00:04:07,000 --> 00:04:09,190 with the keyword return. 50 51 00:04:09,220 --> 00:04:13,940 So now that we've updated our code, our error goes away 51 52 00:04:13,960 --> 00:04:20,630 and when I click run, we do in fact get 6.28318 printed over here. 52 53 00:04:20,860 --> 00:04:24,880 And remember the print statement is inside our main function. 53 54 00:04:25,270 --> 00:04:30,040 So in order for it to get a hold of this result, it had to call my function. 54 55 00:04:30,040 --> 00:04:38,290 So we had to run these two lines of code and then the result of pi * 2 gets outputed from my function, 55 56 00:04:38,560 --> 00:04:44,010 transported back in here and assigned to that variable result. 56 57 00:04:44,060 --> 00:04:51,920 And this is how we're able to print it into the console. So this is how we would solve the problem that 57 58 00:04:51,920 --> 00:04:53,780 we have with our Xylophone app. 58 59 00:04:54,080 --> 00:04:58,240 But how exactly do functions that have outputs work? 59 60 00:04:58,250 --> 00:05:05,140 Let's explore this in a little bit more detail. Previously when we talked about the second type of function, 60 61 00:05:05,140 --> 00:05:07,580 so functions that can take inputs, 61 62 00:05:07,630 --> 00:05:15,370 we saw that we could put a data type inside the parentheses and give the input a name, so that when we 62 63 00:05:15,370 --> 00:05:22,510 call getMilk we could provide a value here and that value will then be used inside the function or 63 64 00:05:22,510 --> 00:05:24,490 inside the curly braces. 64 65 00:05:24,490 --> 00:05:30,850 So in this case for example, our getMilk function takes a value, which is the amount of money that the 65 66 00:05:30,850 --> 00:05:34,450 robot got given to buy a single bottle of milk, 66 67 00:05:34,450 --> 00:05:40,330 and that value gets used to calculate the amount of change that the robot should get back. Which is the 67 68 00:05:40,330 --> 00:05:45,760 amount of money that it got given minus the price of a single bottle of milk. 68 69 00:05:45,760 --> 00:05:47,410 So money minus two. 69 70 00:05:47,410 --> 00:05:50,290 And we end up with the change. 70 71 00:05:50,290 --> 00:05:54,370 Now how can we get this change out of this function? 71 72 00:05:54,400 --> 00:06:01,460 Well in that case, we have to use the third type of functions, which are functions that can have a output. 72 73 00:06:01,630 --> 00:06:07,120 And in order for our function to have an output, we have to change the way that we created a little bit. 73 74 00:06:07,600 --> 00:06:13,810 Instead of having void, which marks the function as having no outputs, we change it to the data type that 74 75 00:06:13,810 --> 00:06:18,210 we expect to come out of the function, which in this case can be an integer. 75 76 00:06:18,310 --> 00:06:26,050 And we also specify what should come out of the function by using a keyword, which is return. 76 77 00:06:26,230 --> 00:06:31,450 So in this case we're saying the output of this function is change. 77 78 00:06:31,450 --> 00:06:38,950 So then if we gave our robot five coins and told it to buy some milk, it's the equivalent of calling 78 79 00:06:39,120 --> 00:06:44,560 our getMilk function passing in the number five, robot buys us a bottle of milk. 79 80 00:06:44,710 --> 00:06:51,610 And if the bottle milk costs 2 coins, then it gives us 3 coins back as the change. 80 81 00:06:51,610 --> 00:06:58,900 And so this expression over here, where it says getMilk passing in an input of 5, because it's able 81 82 00:06:58,900 --> 00:07:05,650 to have an output, once this line of code runs it will be equivalent to 3. 82 83 00:07:05,740 --> 00:07:12,930 So we could assign the output of getMilk to a variable called result. 83 84 00:07:13,030 --> 00:07:18,340 And in this case result would be equal to the output which is 3. 84 85 00:07:18,340 --> 00:07:25,790 So the result is equal to 3 because getMilk, starting with 5 coins is equal to 3. So 85 86 00:07:25,790 --> 00:07:29,460 in this case when we're creating the function, this is what it looks like. 86 87 00:07:29,630 --> 00:07:31,550 And when we're calling the function, 87 88 00:07:31,550 --> 00:07:34,090 this is what it would look like. 88 89 00:07:34,100 --> 00:07:41,030 This function has both an input, which is specified in between the parentheses, and also it can have an 89 90 00:07:41,120 --> 00:07:45,750 output which is specified after the keyword return. 90 91 00:07:45,950 --> 00:07:54,070 That keyword return marks whatever comes after it as the output of the function, and the int keyword 91 92 00:07:54,350 --> 00:08:01,630 at the beginning of our function tells us what is going to be the data type of the output of this function. 92 93 00:08:01,640 --> 00:08:08,000 So when we call the function, we can simply call it by its name passing in any inputs if it takes any. 93 94 00:08:08,060 --> 00:08:15,830 And once that gets carried out, the result or in this case money - 2, gets assigned to this variable. 94 95 00:08:17,000 --> 00:08:22,770 This is how the values would move through the function. If we were calling getMilk passing in the value 95 96 00:08:22,790 --> 00:08:28,650 5, 5 goes into the input and it gets used inside the function. 96 97 00:08:28,700 --> 00:08:37,160 So we end up with 5 - 2 as the return which is equal to 3 of course. And 3 is definitely 97 98 00:08:37,190 --> 00:08:38,110 a whole number 98 99 00:08:38,150 --> 00:08:43,340 so it fits the integer data type that we specified when we created the function. 99 100 00:08:43,340 --> 00:08:50,600 So that becomes the result of getMilk passing in an input of 5, and it replaces the right hand side 100 101 00:08:50,720 --> 00:08:52,000 of the equation. 101 102 00:08:52,010 --> 00:08:56,040 So now we have a variable called change which is equal to 3. 102 103 00:08:56,060 --> 00:08:59,550 So at this stage, we've seen all three types of functions. 103 104 00:08:59,570 --> 00:09:01,110 We've seen the simplest type, 104 105 00:09:01,220 --> 00:09:08,900 the vanilla function, which basically just packages a whole set of instructions together into a function. 105 106 00:09:08,960 --> 00:09:13,630 And whenever we call the function, we specify its name and a set of parentheses, 106 107 00:09:13,760 --> 00:09:19,310 then it will go into the curly braces of where this was created and execute all the instructions in 107 108 00:09:19,310 --> 00:09:21,540 that line by line. 108 109 00:09:21,560 --> 00:09:27,140 This is kind of just like a machine that does something, right? It does some calculations or it does some 109 110 00:09:27,140 --> 00:09:31,370 processing, but essentially there's no input and no output. 110 111 00:09:31,370 --> 00:09:34,280 Now the next type of function gets a little bit more exciting, 111 112 00:09:34,280 --> 00:09:40,910 this is our chocolate flavor function. And it now can take inputs and it takes those in between our parentheses. 112 113 00:09:41,210 --> 00:09:47,180 We have to specify a data type for the input so that when we call the function, we can give it a piece 113 114 00:09:47,180 --> 00:09:54,320 of data that matches that data type, and that number or that input gets used inside the function to do 114 115 00:09:54,320 --> 00:09:58,330 something or make this function more customized. 115 116 00:09:58,610 --> 00:10:06,500 And if this was a machine, then it's kind of like passing in a value into the machine so that it processes 116 117 00:10:06,500 --> 00:10:13,400 the data a little bit differently and you can end up with some custom functionality. So that if the input 117 118 00:10:13,400 --> 00:10:18,710 is different, then the functionality will also be a little bit different each time. 118 119 00:10:19,580 --> 00:10:25,370 Now this leads us to our third type of functions and the most advanced type of functions that we've 119 120 00:10:25,370 --> 00:10:31,670 seen so far. And this is where we can have both an input which goes into the parentheses as well as an 120 121 00:10:31,700 --> 00:10:38,930 output which we specify in the beginning, and also we specify inside the function to say what should 121 122 00:10:38,930 --> 00:10:40,540 be the output. 122 123 00:10:40,820 --> 00:10:47,420 And this is equivalent to a fully fledged machine where not only can you provide an input, but you can 123 124 00:10:47,420 --> 00:10:51,410 also get something that comes out of the machine, as an output. 14431

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