Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
0
1
00:00:00,250 --> 00:00:00,570
All right.
1
2
00:00:00,600 --> 00:00:04,680
So in the last module, we talked about Dart functions.
2
3
00:00:04,680 --> 00:00:10,920
We talked about part one of Dart function. And those were the simplest of functions.
3
4
00:00:11,010 --> 00:00:18,750
They simply package a whole bunch of code together inside some curly braces, so that when you want all
4
5
00:00:18,750 --> 00:00:25,850
of these instructions to be executed in one go, you can simply call the function by specifying its name.
5
6
00:00:26,550 --> 00:00:30,260
But it's rather curious how we've been using these parentheses.
6
7
00:00:30,320 --> 00:00:32,210
We've always left it empty.
7
8
00:00:32,250 --> 00:00:33,600
What are they for?
8
9
00:00:33,600 --> 00:00:39,870
Well I like to think of these types of functions as the most basic type of functions.
9
10
00:00:39,870 --> 00:00:46,080
And there's actually three different levels of functions if you will. If this is the most basic, then
10
11
00:00:46,080 --> 00:00:49,470
let's call it the vanilla flavor of function.
11
12
00:00:49,500 --> 00:00:55,710
Now we're coming across the next flavor of functions. And we saw this in the code in the last lesson.
12
13
00:00:56,250 --> 00:01:05,610
We saw that we could specify an input to our function inside those parentheses, and we could use that
13
14
00:01:05,610 --> 00:01:08,470
input inside the function.
14
15
00:01:08,640 --> 00:01:15,990
And this makes our function far more adaptable because we can now get it to do slightly different things
15
16
00:01:16,320 --> 00:01:19,520
by providing a different input.
16
17
00:01:19,530 --> 00:01:27,270
So now let's talk about part two of Dart functions. We're leveling up our Dart functions. So coming back
17
18
00:01:27,270 --> 00:01:28,890
to our little robot,
18
19
00:01:29,100 --> 00:01:33,690
what if instead of just telling him to go and get milk, we could actually tell him how many bottles of
19
20
00:01:33,690 --> 00:01:41,340
milk to get? And we would do that by calling the function getMilk and providing an input inbetween
20
21
00:01:41,340 --> 00:01:42,450
the parentheses.
21
22
00:01:42,450 --> 00:01:46,610
So if we wanted two bottles, we would write 2,if we want four bottles we would write 4.
22
23
00:01:46,950 --> 00:01:50,550
So that means that our little robot is a little bit more intelligent
23
24
00:01:50,550 --> 00:01:58,170
now. We don't have to always get one bottle of milk every single time it goes to the store. Instead of
24
25
00:01:58,170 --> 00:02:03,510
having a function that looks like this where it's just a whole bunch of instructions packaged together
25
26
00:02:03,510 --> 00:02:05,710
into a block with a name,
26
27
00:02:05,730 --> 00:02:14,970
we now have some inputs inside the parentheses. And those inputs can be used inside the block of code
27
28
00:02:15,330 --> 00:02:20,000
to change the functionality of that block of code.
28
29
00:02:20,160 --> 00:02:25,020
And now we can use that input inside the function.
29
30
00:02:25,020 --> 00:02:30,310
For example we could calculate the cost of getting X number of bottles, right?
30
31
00:02:30,600 --> 00:02:34,460
If they called getMilk and they wanted two bottles of milk,
31
32
00:02:34,470 --> 00:02:41,040
then if one bottle of milk was £1.5, then that would be £3 for the total cost of
32
33
00:02:41,040 --> 00:02:41,930
the milk.
33
34
00:02:42,090 --> 00:02:50,160
Basically, we now have this input or some might call an argument, and this is something that will modify
34
35
00:02:50,160 --> 00:02:55,320
the functionality of this function. But there's two important parts to this.
35
36
00:02:55,380 --> 00:03:02,310
One is you have to specify the data type that goes into this function, and you also have to give it a
36
37
00:03:02,310 --> 00:03:08,880
name so that you can refer to it when you need it inside the curly braces of this block of code.
37
38
00:03:08,880 --> 00:03:12,990
Now when you call the function, it also looks a little bit different.
38
39
00:03:13,050 --> 00:03:18,780
We still call it by its name, but inside the parentheses instead of having an empty set up parentheses,
39
40
00:03:19,140 --> 00:03:20,590
we give it an input.
40
41
00:03:20,700 --> 00:03:23,610
So let's say we want to get two bottles of milk.
41
42
00:03:23,610 --> 00:03:33,330
Well then the input now flows in to the function, and it becomes stored inside this name called bottles.
42
43
00:03:33,330 --> 00:03:38,940
Now the first thing it checks is to make sure that it definitely is getting the right data type as the
43
44
00:03:38,940 --> 00:03:41,100
input. So it's definitely getting an integer
44
45
00:03:41,100 --> 00:03:43,880
in this case, and 2 is a whole number,
45
46
00:03:43,920 --> 00:03:45,180
so that's completely fine.
46
47
00:03:46,350 --> 00:03:54,690
So when we call getMilk passing in the input of 2, it's almost like we're creating a new variable
47
48
00:03:54,840 --> 00:03:56,770
that is of data type int.
48
49
00:03:56,940 --> 00:04:01,200
It has a name of bottle, and it has a value of 2.
49
50
00:04:01,860 --> 00:04:10,350
So now inside the curly braces of this function, we get to use this new variable, including to calculate
50
51
00:04:10,350 --> 00:04:14,660
the cost or do whatever we will with that input.
51
52
00:04:14,670 --> 00:04:21,690
Now it's your part to apply this in practice. Head over to a dartpad and I want you to create a greeting
52
53
00:04:21,690 --> 00:04:22,920
robot.
53
54
00:04:23,010 --> 00:04:25,640
Now this greeting robot is pretty simple.
54
55
00:04:25,740 --> 00:04:35,450
All it needs to do is to print a greeting such as 'Hello Angela' to the console. Notice here, I'm calling
55
56
00:04:35,450 --> 00:04:41,840
a function called greet, and I'm passing in the input, that's my name, but it could also be anybody else's
56
57
00:04:41,840 --> 00:04:51,980
name. And this name in here has to be put after a 'hello' or a 'how do you?' do or 'howdy'.
57
58
00:04:51,980 --> 00:04:54,450
And it has to be printed into the console.
58
59
00:04:54,530 --> 00:05:02,360
So go ahead and create this great function that achieves this functionality. Pause a video and complete
59
60
00:05:02,390 --> 00:05:03,100
this challenge.
60
61
00:05:06,180 --> 00:05:06,510
All right.
61
62
00:05:06,530 --> 00:05:10,790
So we start out creating our function with the keyword void.
62
63
00:05:11,010 --> 00:05:12,710
And then we give our function a name.
63
64
00:05:12,720 --> 00:05:14,640
So in this case it's going to be called greet,
64
65
00:05:15,270 --> 00:05:18,590
because that's how we said we would be using it.
65
66
00:05:18,630 --> 00:05:22,160
Now we have a set of parentheses, and inside here,
66
67
00:05:22,170 --> 00:05:26,510
we specify the argument or the inputs to this function.
67
68
00:05:26,520 --> 00:05:29,580
Now in this case, we want to be able to pass in a name.
68
69
00:05:29,580 --> 00:05:35,790
So that later on, we can simply execute the functionality, that's print. And we can say something like hello
69
70
00:05:36,240 --> 00:05:43,650
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
71
00:05:43,650 --> 00:05:47,520
function will accept is a string, because it's going to be a name,
71
72
00:05:47,520 --> 00:05:47,730
right?
72
73
00:05:47,730 --> 00:05:51,190
It'll be Angela or Jack Bauer or whatever it may be.
73
74
00:05:51,390 --> 00:05:56,520
And then we have to give that a name, and we'll call it personToGreet.
74
75
00:05:57,540 --> 00:06:02,910
And now, we get to use this personToGreet inside our function.
75
76
00:06:02,910 --> 00:06:10,320
So every time I say inside, it just means inside the curly braces. And we can use our $ to add
76
77
00:06:10,350 --> 00:06:19,650
that personToGreet to the end of our hello sentence. And let's cap it off with a semicolon.
77
78
00:06:19,680 --> 00:06:25,250
And now when I hit run, it will go into main to see what it needs to do.
78
79
00:06:25,440 --> 00:06:32,640
And we tell it to call the greet function, so it finds this great function. We give it the input or an
79
80
00:06:32,670 --> 00:06:34,330
argument of Jack Bauer.
80
81
00:06:34,350 --> 00:06:41,750
So Jack Bauer then goes inside here, and now personToGreet is equal to Jack Bauer.
81
82
00:06:41,790 --> 00:06:48,550
So now inside this function when it looks to see what it needs to do, it'll print out the words hello
82
83
00:06:48,780 --> 00:06:53,120
and the personToGreet, which in this case is Jack Bauer.
83
84
00:06:53,220 --> 00:06:57,000
So it will simply replace it right here.
84
85
00:06:57,000 --> 00:07:02,730
And so now effectively, we've done the same thing as write print, Hello Jack Bauer.
85
86
00:07:02,730 --> 00:07:11,370
But it's so much more flexible because we can specify any input and it will simply run it through the
86
87
00:07:11,370 --> 00:07:14,180
same process.
87
88
00:07:14,210 --> 00:07:19,310
I hope that makes sense. But you might also realize that there's a bit of a weakness here.
88
89
00:07:19,310 --> 00:07:21,500
What if we had two things here?
89
90
00:07:21,500 --> 00:07:24,830
What if we said we had another greeting,
90
91
00:07:24,830 --> 00:07:25,480
right?
91
92
00:07:25,490 --> 00:07:26,860
What if, you know, I wanted to be able to
92
93
00:07:26,870 --> 00:07:30,050
define what I say to this person?
93
94
00:07:30,050 --> 00:07:35,150
Say I want to be able to say, you know, instead of hello, I might want to say good bye,
94
95
00:07:35,150 --> 00:07:35,450
right?
95
96
00:07:35,480 --> 00:07:43,730
So let's use our greeting variable here, and then we've got our personToGreet here. And here, I can simply
96
97
00:07:43,730 --> 00:07:51,570
write Jackie and I'll say howdy. And I'm going to separate them with a comma.
97
98
00:07:51,570 --> 00:07:57,390
So now if we click run, you can see that what gets printed is Howdy Jackie.
98
99
00:07:57,390 --> 00:08:03,180
So if I say how do you do, then our function ends up printing
99
100
00:08:03,180 --> 00:08:04,220
How do you do
100
101
00:08:04,230 --> 00:08:05,170
Jackie.
101
102
00:08:05,610 --> 00:08:11,060
We can use as many inputs as we want but at some point it gets a bit confusing right?
102
103
00:08:11,070 --> 00:08:15,500
We're calling this function, which one should go where? Is it
103
104
00:08:15,510 --> 00:08:17,040
this one that should go first
104
105
00:08:17,040 --> 00:08:19,370
or is it the other one that has to go first?
105
106
00:08:19,530 --> 00:08:26,790
And notice that this actually matters, the order matters because. If I click run, now it'll instead print
106
107
00:08:27,090 --> 00:08:28,140
Jackie
107
108
00:08:28,140 --> 00:08:29,270
How do you do.
108
109
00:08:29,320 --> 00:08:29,670
Right?
109
110
00:08:29,730 --> 00:08:38,390
The first part is this greeting, and the second part is this personToGreet. So to minimize confusion,
110
111
00:08:38,490 --> 00:08:46,650
one of the things that you can do is to add this name into the greet function as an argument name.
111
112
00:08:46,770 --> 00:08:49,500
So then you would call this function like this.
112
113
00:08:49,620 --> 00:08:52,560
You would say greet with the greeting,
113
114
00:08:52,590 --> 00:08:54,090
that is How do you do.
114
115
00:08:54,240 --> 00:08:59,120
And with the personToGreet being this one.
115
116
00:08:59,370 --> 00:09:04,290
And when we've been using a lot of functions that came from the Flutter team, this is what we've been
116
117
00:09:04,290 --> 00:09:14,880
seeing. Now in order for this to work, all you have to do is just add a set of curly braces around your
117
118
00:09:14,880 --> 00:09:24,420
input parameters. And that allows you to use these names that you've defined in the input when you call
118
119
00:09:24,420 --> 00:09:28,250
the function as well as when you're inside the function.
119
120
00:09:28,320 --> 00:09:35,390
And this means that there's less confusion when you're actually trying to trigger the functionality.
120
121
00:09:35,490 --> 00:09:41,090
So whereas previously we simply had the data type and the name of the input argument,
121
122
00:09:41,250 --> 00:09:50,140
now we're adding some curly braces around the input arguments where we want to use their name.
122
123
00:09:50,190 --> 00:09:56,250
And so now what when I call this getMilk function, it won't let me give it an input unless I specified
123
124
00:09:56,640 --> 00:09:59,480
which argument the input was for.
124
125
00:09:59,700 --> 00:10:04,520
But it also means I can swap things around now and the order no longer matters.
125
126
00:10:04,620 --> 00:10:10,630
I can say that the personToGreet is Jackie and the greeting is going to be how do you do.
126
127
00:10:10,650 --> 00:10:19,290
And now if I hit run, it achieves exactly the same thing. But because I now can associate my data with
127
128
00:10:19,290 --> 00:10:20,540
the argument,
128
129
00:10:20,760 --> 00:10:26,570
it's much much clearer what's actually happening with my function.
129
130
00:10:26,660 --> 00:10:32,180
So now that you've learned all about functions in Dart, let's head back to completing our Xylophone app.
13995
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.