Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
0
1
00:00:00,290 --> 00:00:00,600
All right.
1
2
00:00:00,630 --> 00:00:06,630
So now that you've learned about all three types of functions, it's time to challenge yourself with an
2
3
00:00:06,630 --> 00:00:07,710
exercise.
3
4
00:00:07,740 --> 00:00:14,190
And in this case, I've created a pre-built dartpad for you with some code that I've already written.
4
5
00:00:14,640 --> 00:00:21,170
And you can access this exact same dartpad by heading over to this URL which I will of course linked
5
6
00:00:21,170 --> 00:00:24,360
to in the course resources for this lesson.
6
7
00:00:24,360 --> 00:00:28,730
Now once you get here, you can see that we have a challenge for you.
7
8
00:00:28,890 --> 00:00:36,240
And the idea is that without changing this main function so without touching any of these lines of code
8
9
00:00:36,240 --> 00:00:37,710
that are already written,
9
10
00:00:37,710 --> 00:00:45,370
I want you to make this code work and get the result printed in the console. In order to achieve this,
10
11
00:00:45,390 --> 00:00:52,410
you're going to need to create a add function and also a multiply function, because at the moment, this
11
12
00:00:52,410 --> 00:00:56,070
is what our Dart analysis is telling us that doesn't exist.
12
13
00:00:56,100 --> 00:00:59,250
So there's no add function and there's no multiply function.
13
14
00:00:59,250 --> 00:01:06,210
So somewhere down here after the main function, create these two functions, add and multiply.
14
15
00:01:06,210 --> 00:01:13,290
Making sure that you spell them exactly the same and also match up the input parameter names where there
15
16
00:01:13,290 --> 00:01:14,370
are any.
16
17
00:01:14,370 --> 00:01:21,210
Once you've completed the challenge, firstly all the errors should go away and it should now look like
17
18
00:01:21,210 --> 00:01:22,170
this.
18
19
00:01:22,170 --> 00:01:28,470
And when you click run, it should print the result which in this case should be 23.3
19
20
00:01:28,500 --> 00:01:36,060
recurring into the console. The way that we're able to calculate this is because our add function adds
20
21
00:01:36,150 --> 00:01:40,590
our two numbers, where we give it as inputs 5 and 9 together.
21
22
00:01:40,650 --> 00:01:42,580
So 5 + 9 = 14.
22
23
00:01:42,660 --> 00:01:47,820
And then that 14 becomes the result of this right hand side,
23
24
00:01:47,880 --> 00:01:54,550
and it goes into the next step where we tried to multiply 14 by 5.
24
25
00:01:54,570 --> 00:02:00,000
So there's multiply function has to multiply this value by this second value.
25
26
00:02:00,870 --> 00:02:04,640
I've obviously hidden what the steps are in order to achieve this
26
27
00:02:04,740 --> 00:02:08,680
so that you can test yourself and see if you can make this work.
27
28
00:02:08,730 --> 00:02:12,330
So pause the video now and try to complete this challenge.
28
29
00:02:15,640 --> 00:02:15,990
All right.
29
30
00:02:15,990 --> 00:02:17,340
So how did you get on?
30
31
00:02:17,370 --> 00:02:22,080
Well in order to complete this challenge, you need to have a good grasp of functions.
31
32
00:02:22,110 --> 00:02:29,340
So the first thing we'll address here is that right here, we are creating a new variable that has a data
32
33
00:02:29,340 --> 00:02:30,450
type of int,
33
34
00:02:30,480 --> 00:02:38,240
so a whole number. And it's going to be storing something that comes out as the output of this function.
34
35
00:02:38,370 --> 00:02:44,600
The function has to be called add, and it takes two inputs, n1 and n2,
35
36
00:02:44,640 --> 00:02:52,440
and in this case we're saying 5 and 9. And inside this function obviously we need to add 5 to 9
36
37
00:02:52,620 --> 00:02:55,680
in order for it to come out as our result.
37
38
00:02:56,040 --> 00:02:58,400
So let's create our function now.
38
39
00:02:58,620 --> 00:03:01,140
Previously we've always started with void.
39
40
00:03:01,140 --> 00:03:03,780
In this case because we want to have an output,
40
41
00:03:03,840 --> 00:03:06,900
and in this case we want to output a whole number,
41
42
00:03:06,900 --> 00:03:12,030
we're going to start with the keyword int. And then we're going to provide the name of our function, which
42
43
00:03:12,030 --> 00:03:13,080
is called add.
43
44
00:03:13,350 --> 00:03:21,240
And you can see that once I put in that word, then my error for the add function goes away. This is the
44
45
00:03:21,240 --> 00:03:27,880
bare bones of my function so far. But I now need to say that it provides some inputs.
45
46
00:03:28,200 --> 00:03:35,640
And in this case because we're able to call add and provide our inputs with named parameters or named
46
47
00:03:35,670 --> 00:03:44,610
arguments, we have to add in those curly braces in here so that we can write int n1 and int n2.
47
48
00:03:44,610 --> 00:03:52,800
So what this does is it says that when you call the function add, you can provide the name of each parameter,
48
49
00:03:53,100 --> 00:03:59,060
just like we have done here. And both inputs are going to be integers,
49
50
00:03:59,210 --> 00:04:01,570
so we have the int keyword in there.
50
51
00:04:02,060 --> 00:04:09,380
And then inside the function, we're simply going to add n1 to n2. So you can either create a variable
51
52
00:04:09,410 --> 00:04:18,260
called result and you write equals n1 + n2, and then you can output the result by using that
52
53
00:04:18,260 --> 00:04:19,490
return keyword.
53
54
00:04:19,640 --> 00:04:27,870
So you write return result. And this will get rid of all our errors to do with our step 1.
54
55
00:04:27,890 --> 00:04:33,530
Now if this makes more sense to you, then be sure to write it like this, but you can also cut this down
55
56
00:04:33,560 --> 00:04:37,600
by one line. By instead of having to create that variable,
56
57
00:04:37,640 --> 00:04:45,560
you can also just simply return n1 + n2. And we can remove this line entirely.
57
58
00:04:45,560 --> 00:04:52,670
So that does exactly the same thing but it combined that into one step. And we don't have to create that
58
59
00:04:52,760 --> 00:04:55,910
intermediate step that we had previously.
59
60
00:04:56,030 --> 00:05:01,020
Now that add is done, we have to create our multiply function.
60
61
00:05:01,370 --> 00:05:04,280
Let's go down here and create our multiply.
61
62
00:05:04,350 --> 00:05:11,690
It's also going to have an output of integer type because we can see that the variable that we're using
62
63
00:05:12,050 --> 00:05:17,030
to hold the data that comes out of this function, has the data type int.
63
64
00:05:17,120 --> 00:05:20,340
So this must have an output of int.
64
65
00:05:20,480 --> 00:05:24,490
So we're going to start off with int, and then we're going to give it a name.
65
66
00:05:24,500 --> 00:05:30,890
So it's called multiply, making sure those are spelled the same. But in this case, do you see that we don't
66
67
00:05:30,890 --> 00:05:34,810
have the argument names when we call the function?
67
68
00:05:34,880 --> 00:05:39,170
So we didn't have to write n1: or n2:.
68
69
00:05:39,170 --> 00:05:45,590
So that means we don't need those curly braces and we can simply just specify our data type and the name
69
70
00:05:45,590 --> 00:05:52,830
of our argument, which will again call n1 and n2. And they are also going to be integers.
70
71
00:05:52,940 --> 00:06:02,030
Now inside our multiply, we're going to of course return the result of multiplying n1 by n2. And
71
72
00:06:02,090 --> 00:06:09,500
this will come out of the multiply function as the output. And that output goes into here.
72
73
00:06:09,500 --> 00:06:16,490
So the output is gonna be equal to the step 2 result which then gets used in the next step of calculations
73
74
00:06:16,790 --> 00:06:21,190
where it gets divided by 3 and finally that final result is printed.
74
75
00:06:21,560 --> 00:06:27,920
So now when we hit run, we can see the final output is 23.3 recurring, and this is
75
76
00:06:27,920 --> 00:06:30,510
how you would solve this challenge.
76
77
00:06:30,590 --> 00:06:34,610
Now if any point of this challenge is confusing,
77
78
00:06:34,610 --> 00:06:41,070
be sure to revisit the lessons where we talked about functions in Dart part 1, part 2 and Part 3.
78
79
00:06:41,150 --> 00:06:46,610
But otherwise, we're going to get back to coding up our Xylophone app and trying to solve the problem
79
80
00:06:46,610 --> 00:06:49,600
that we had earlier on using our newfound knowledge.
80
81
00:06:49,640 --> 00:06:52,310
So all of that and more, I'll see you on the next lesson.
9038
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.