Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
0 All right. 1
So in the last module, we talked about Dart functions. 2
We talked about part one of Dart function. And those were the simplest of functions. 3
They simply package a whole bunch of code together inside some curly braces, so that when you want all 4
of these instructions to be executed in one go, you can simply call the function by specifying its name. 5
But it's rather curious how we've been using these parentheses. 6
We've always left it empty. 7
What are they for? 8
Well I like to think of these types of functions as the most basic type of functions. 9
And there's actually three different levels of functions if you will. If this is the most basic, then 10
let's call it the vanilla flavor of function. 11
Now we're coming across the next flavor of functions. And we saw this in the code in the last lesson. 12
We saw that we could specify an input to our function inside those parentheses, and we could use that 13
input inside the function. 14
And this makes our function far more adaptable because we can now get it to do slightly different things 15
by providing a different input. 16
So now let's talk about part two of Dart functions. We're leveling up our Dart functions. So coming back 17
to our little robot, 18
what if instead of just telling him to go and get milk, we could actually tell him how many bottles of 19
milk to get? And we would do that by calling the function getMilk and providing an input inbetween 20
the parentheses. 21
So if we wanted two bottles, we would write 2,if we want four bottles we would write 4. 22
So that means that our little robot is a little bit more intelligent 23
now. We don't have to always get one bottle of milk every single time it goes to the store. Instead of 24
having a function that looks like this where it's just a whole bunch of instructions packaged together 25
into a block with a name, 26
we now have some inputs inside the parentheses. And those inputs can be used inside the block of code 27
to change the functionality of that block of code. 28
And now we can use that input inside the function. 29
For example we could calculate the cost of getting X number of bottles, right? 30
If they called getMilk and they wanted two bottles of milk, 31
then if one bottle of milk was £1.5, then that would be £3 for the total cost of 32
the milk. 33
Basically, we now have this input or some might call an argument, and this is something that will modify 34
the functionality of this function. But there's two important parts to this. 35
One is you have to specify the data type that goes into this function, and you also have to give it a 36
name so that you can refer to it when you need it inside the curly braces of this block of code. 37
Now when you call the function, it also looks a little bit different. 38
We still call it by its name, but inside the parentheses instead of having an empty set up parentheses, 39
we give it an input. 40
So let's say we want to get two bottles of milk. 41
Well then the input now flows in to the function, and it becomes stored inside this name called bottles. 42
Now the first thing it checks is to make sure that it definitely is getting the right data type as the 43
input. So it's definitely getting an integer 44
in this case, and 2 is a whole number, 45
so that's completely fine. 46
So when we call getMilk passing in the input of 2, it's almost like we're creating a new variable 47
that is of data type int. 48
It has a name of bottle, and it has a value of 2. 49
So now inside the curly braces of this function, we get to use this new variable, including to calculate 50
the cost or do whatever we will with that input. 51
Now it's your part to apply this in practice. Head over to a dartpad and I want you to create a greeting 52
robot. 53
Now this greeting robot is pretty simple. 54
All it needs to do is to print a greeting such as 'Hello Angela' to the console. Notice here, I'm calling 55
a function called greet, and I'm passing in the input, that's my name, but it could also be anybody else's 56
name. And this name in here has to be put after a 'hello' or a 'how do you?' do or 'howdy'. 57
And it has to be printed into the console. 58
So go ahead and create this great function that achieves this functionality. Pause a video and complete 59
this challenge. 60
All right. 61
So we start out creating our function with the keyword void. 62
And then we give our function a name. 63
So in this case it's going to be called greet, 64
because that's how we said we would be using it. 65
Now we have a set of parentheses, and inside here, 66
we specify the argument or the inputs to this function. 67
Now in this case, we want to be able to pass in a name. 68
So that later on, we can simply execute the functionality, that's print. And we can say something like hello 69
and we might be able to add a name into there. So that we'll go in here. And the data type that our greet 70
function will accept is a string, because it's going to be a name, 71
right? 72
It'll be Angela or Jack Bauer or whatever it may be. 73
And then we have to give that a name, and we'll call it personToGreet. 74
And now, we get to use this personToGreet inside our function. 75
So every time I say inside, it just means inside the curly braces. And we can use our $ to add 76
that personToGreet to the end of our hello sentence. And let's cap it off with a semicolon. 77
And now when I hit run, it will go into main to see what it needs to do. 78
And we tell it to call the greet function, so it finds this great function. We give it the input or an 79
argument of Jack Bauer. 80
So Jack Bauer then goes inside here, and now personToGreet is equal to Jack Bauer. 81
So now inside this function when it looks to see what it needs to do, it'll print out the words hello 82
and the personToGreet, which in this case is Jack Bauer. 83
So it will simply replace it right here. 84
And so now effectively, we've done the same thing as write print, Hello Jack Bauer. 85
But it's so much more flexible because we can specify any input and it will simply run it through the 86
same process. 87
I hope that makes sense. But you might also realize that there's a bit of a weakness here. 88
What if we had two things here? 89
What if we said we had another greeting, 90
right? 91
What if, you know, I wanted to be able to 92
define what I say to this person? 93
Say I want to be able to say, you know, instead of hello, I might want to say good bye, 94
right? 95
So let's use our greeting variable here, and then we've got our personToGreet here. And here, I can simply 96
write Jackie and I'll say howdy. And I'm going to separate them with a comma. 97
So now if we click run, you can see that what gets printed is Howdy Jackie. 98
So if I say how do you do, then our function ends up printing 99
How do you do 100
Jackie. 101
We can use as many inputs as we want but at some point it gets a bit confusing right? 102
We're calling this function, which one should go where? Is it 103
this one that should go first 104
or is it the other one that has to go first? 105
And notice that this actually matters, the order matters because. If I click run, now it'll instead print 106
Jackie 107
How do you do. 108
Right? 109
The first part is this greeting, and the second part is this personToGreet. So to minimize confusion, 110
one of the things that you can do is to add this name into the greet function as an argument name. 111
So then you would call this function like this. 112
You would say greet with the greeting, 113
that is How do you do. 114
And with the personToGreet being this one. 115
And when we've been using a lot of functions that came from the Flutter team, this is what we've been 116
seeing. Now in order for this to work, all you have to do is just add a set of curly braces around your 117
input parameters. And that allows you to use these names that you've defined in the input when you call 118
the function as well as when you're inside the function. 119
And this means that there's less confusion when you're actually trying to trigger the functionality. 120
So whereas previously we simply had the data type and the name of the input argument, 121
now we're adding some curly braces around the input arguments where we want to use their name. 122
And so now what when I call this getMilk function, it won't let me give it an input unless I specified 123
which argument the input was for. 124
But it also means I can swap things around now and the order no longer matters. 125
I can say that the personToGreet is Jackie and the greeting is going to be how do you do. 126
And now if I hit run, it achieves exactly the same thing. But because I now can associate my data with 127
the argument, 128
it's much much clearer what's actually happening with my function. 129
So now that you've learned all about functions in Dart, let's head back to completing our Xylophone app.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.