Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
0
1
00:00:00,630 --> 00:00:09,150
Now in the last lesson, we covered flat buttons and the void callback that they expect.
1
2
00:00:09,150 --> 00:00:15,360
We also used the print function and gave it an input, or rather an argument.
2
3
00:00:15,360 --> 00:00:19,710
So in this lesson, I wants to quickly cover Dart functions.
3
4
00:00:19,710 --> 00:00:26,910
So if you already know what an argument is, what return does and what a function does, then feel free to
4
5
00:00:26,910 --> 00:00:32,140
skip this lesson and head over to the next one where we continue building out our Flutter app.
5
6
00:00:32,220 --> 00:00:38,580
But for those guys who are staying with me, functions as you can guess from the name, are there to give
6
7
00:00:38,580 --> 00:00:43,790
your app or you give your program some functionality, right? Now,
7
8
00:00:43,800 --> 00:00:49,830
in this lesson, I want to talk about functions in the most fundamental sense and when we using functions
8
9
00:00:49,920 --> 00:00:51,000
as programmers,
9
10
00:00:51,180 --> 00:00:59,730
fundamentally, we're trying to package bits of code together into a block and to call that block of code
10
11
00:00:59,850 --> 00:01:05,340
repeatedly, saving ourselves from typing the same code again and again and again.
11
12
00:01:05,910 --> 00:01:15,330
So for example, if you had a little housekeeping robot, this guy. His name is Bit Bob. And he does a number
12
13
00:01:15,330 --> 00:01:16,320
of things right?
13
14
00:01:16,320 --> 00:01:22,020
For example he'll go and get you milk every day. But you have to tell him how to do it.
14
15
00:01:22,050 --> 00:01:28,940
You have to tell him to leave the house, move right to blocks, move up four blocks, then move right to blocks,
15
16
00:01:28,980 --> 00:01:37,140
get the milk, come back. And you have to specify all these instructions to him because he's a robot and
16
17
00:01:37,140 --> 00:01:44,420
he doesn't understand contextual information like go to the store and get me some milk. Now if you had
17
18
00:01:44,420 --> 00:01:51,890
to do this day after day after day every single time you wanted milk, your fingers would start hurting,
18
19
00:01:52,010 --> 00:01:55,720
and you wouldn't want to keep typing out the same instructions.
19
20
00:01:55,730 --> 00:01:56,030
Right?
20
21
00:01:56,540 --> 00:01:58,070
So what can you do?
21
22
00:01:58,070 --> 00:01:59,780
Well we're programmers.
22
23
00:01:59,780 --> 00:02:01,630
We're born to be lazy.
23
24
00:02:01,760 --> 00:02:09,980
So what we can do instead, is we can take all these instructions that the robot understands, and we can
24
25
00:02:09,980 --> 00:02:13,620
package it into a block of code.
25
26
00:02:13,880 --> 00:02:22,370
Now a block of code, in many many programming languages, is represented by code that inside a set of curly
26
27
00:02:22,370 --> 00:02:22,970
braces.
27
28
00:02:23,870 --> 00:02:32,420
Once we've got all our instructions inside our curly braces, we can give that block of code a name.
28
29
00:02:32,540 --> 00:02:35,630
And in this case we've called it getMilk.
29
30
00:02:35,720 --> 00:02:43,430
Now every time I need some milk, I can tell the robot get milk and it'll know what to do because it
30
31
00:02:43,430 --> 00:02:48,920
will find this function called getMilk, and look inside it to see what it should do.
31
32
00:02:48,920 --> 00:02:52,030
It's almost like I'm creating a recipe, right?
32
33
00:02:52,040 --> 00:02:56,960
It's like, I want to eat cookies, but I don't want to bake cookies.
33
34
00:02:56,960 --> 00:03:04,250
So let me write down a recipe and give it to my housekeeping robot, so that they can make it for me.
34
35
00:03:04,460 --> 00:03:12,830
And that recipe or the name of the recipe is basically the name of our function. In Dart, the way that
35
36
00:03:12,830 --> 00:03:16,940
you would create the function looks like this.
36
37
00:03:16,940 --> 00:03:23,780
We've got the name of the function which is called getMilk, and then we've got the curly braces which
37
38
00:03:23,780 --> 00:03:29,480
enclose all of the things that should happen when this function gets called.
38
39
00:03:29,480 --> 00:03:35,720
And then later on when we actually need to use it, we will call the function or tell the computer to
39
40
00:03:35,810 --> 00:03:42,950
execute all of this by writing something like this, giving the name of the function.
40
41
00:03:42,980 --> 00:03:46,170
Now this is called a named function.
41
42
00:03:46,230 --> 00:03:48,750
Well it makes sense because it's got a name.
42
43
00:03:48,800 --> 00:03:49,020
Right?
43
44
00:03:49,020 --> 00:03:52,320
We can identify it by what it's called.
44
45
00:03:52,410 --> 00:03:59,850
Now if we dropped everything other than just the core parts, namely the parentheses and the curly braces
45
46
00:03:59,940 --> 00:04:06,560
and all the instructions, then this would become an anonymous function, because it doesn't have a name.
46
47
00:04:06,590 --> 00:04:07,340
Right?
47
48
00:04:07,380 --> 00:04:11,170
And these anonymous functions are legion.
48
49
00:04:11,310 --> 00:04:12,190
No just kidding.
49
50
00:04:12,210 --> 00:04:15,840
These are anonymous functions are actually very useful.
50
51
00:04:15,840 --> 00:04:22,950
They allow us to tell the computer to do something without explicitly having to create this entire function
51
52
00:04:23,040 --> 00:04:31,480
and give it a name and call its name. When we were coding up our onPressed which expected a void callback,
52
53
00:04:32,050 --> 00:04:35,350
it essentially wanted an anonymous function.
53
54
00:04:35,350 --> 00:04:43,530
We passed in the two empty parentheses, and we gave it the block of code that we want to be executed
54
55
00:04:43,660 --> 00:04:50,050
when the button got pressed. This is all you need to know about functions so far to be able to understand
55
56
00:04:50,470 --> 00:04:52,090
what's happening in our flat
56
57
00:04:52,090 --> 00:04:58,240
button. A little bit later on when we level up and we talk about functions in more detail, we'll talk
57
58
00:04:58,240 --> 00:05:05,170
more about what goes in these parentheses namely some inputs into the function. And also we'll talk about
58
59
00:05:05,170 --> 00:05:09,930
the return statement when we want our function to output something.
59
60
00:05:10,060 --> 00:05:14,080
But for now, just know that this is anonymous function.
60
61
00:05:14,080 --> 00:05:18,800
It has a set of parentheses and a set of curly braces. In the curly braces
61
62
00:05:18,880 --> 00:05:26,560
we say what should happen. So now that we're all caught up, in the next lesson we're gonna get back to
62
63
00:05:26,560 --> 00:05:33,550
coding up our dice app. And we're going to get our button to change our dice images to actually make the
63
64
00:05:33,550 --> 00:05:34,460
app work.
64
65
00:05:34,580 --> 00:05:37,510
So for all of that and more, I'll see on the next lesson.
7334
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.