All right.
So now that you've learned about all three types of functions, it's time to challenge yourself with an
exercise.
And in this case, I've created a pre-built dartpad for you with some code that I've already written.
And you can access this exact same dartpad by heading over to this URL which I will of course linked
to in the course resources for this lesson.
Now once you get here, you can see that we have a challenge for you.
And the idea is that without changing this main function so without touching any of these lines of code
that are already written,
I want you to make this code work and get the result printed in the console. In order to achieve this,
you're going to need to create a add function and also a multiply function, because at the moment, this
is what our Dart analysis is telling us that doesn't exist.
So there's no add function and there's no multiply function.
So somewhere down here after the main function, create these two functions, add and multiply.
Making sure that you spell them exactly the same and also match up the input parameter names where there
are any.
Once you've completed the challenge, firstly all the errors should go away and it should now look like
this.
And when you click run, it should print the result which in this case should be 23.3
recurring into the console. The way that we're able to calculate this is because our add function adds
our two numbers, where we give it as inputs 5 and 9 together.
So 5 + 9 = 14.
And then that 14 becomes the result of this right hand side,
and it goes into the next step where we tried to multiply 14 by 5.
So there's multiply function has to multiply this value by this second value.
I've obviously hidden what the steps are in order to achieve this
so that you can test yourself and see if you can make this work.
So pause the video now and try to complete this challenge.
All right.
So how did you get on?
Well in order to complete this challenge, you need to have a good grasp of functions.
So the first thing we'll address here is that right here, we are creating a new variable that has a data
type of int,
so a whole number. And it's going to be storing something that comes out as the output of this function.
The function has to be called add, and it takes two inputs, n1 and n2,
and in this case we're saying 5 and 9. And inside this function obviously we need to add 5 to 9
in order for it to come out as our result.
So let's create our function now.
Previously we've always started with void.
In this case because we want to have an output,
and in this case we want to output a whole number,
we're going to start with the keyword int. And then we're going to provide the name of our function, which
is called add.
And you can see that once I put in that word, then my error for the add function goes away. This is the
bare bones of my function so far. But I now need to say that it provides some inputs.
And in this case because we're able to call add and provide our inputs with named parameters or named
arguments, we have to add in those curly braces in here so that we can write int n1 and int n2.
So what this does is it says that when you call the function add, you can provide the name of each parameter,
just like we have done here. And both inputs are going to be integers,
so we have the int keyword in there.
And then inside the function, we're simply going to add n1 to n2. So you can either create a variable
called result and you write equals n1 + n2, and then you can output the result by using that
return keyword.
So you write return result. And this will get rid of all our errors to do with our step 1.
Now if this makes more sense to you, then be sure to write it like this, but you can also cut this down
by one line. By instead of having to create that variable,
you can also just simply return n1 + n2. And we can remove this line entirely.
So that does exactly the same thing but it combined that into one step. And we don't have to create that
intermediate step that we had previously.
Now that add is done, we have to create our multiply function.
Let's go down here and create our multiply.
It's also going to have an output of integer type because we can see that the variable that we're using
to hold the data that comes out of this function, has the data type int.
So this must have an output of int.
So we're going to start off with int, and then we're going to give it a name.
So it's called multiply, making sure those are spelled the same. But in this case, do you see that we don't
have the argument names when we call the function?
So we didn't have to write n1: or n2:.
So that means we don't need those curly braces and we can simply just specify our data type and the name
of our argument, which will again call n1 and n2. And they are also going to be integers.
Now inside our multiply, we're going to of course return the result of multiplying n1 by n2. And
this will come out of the multiply function as the output. And that output goes into here.
So the output is gonna be equal to the step 2 result which then gets used in the next step of calculations
where it gets divided by 3 and finally that final result is printed.
So now when we hit run, we can see the final output is 23.3 recurring, and this is
how you would solve this challenge.
Now if any point of this challenge is confusing,
be sure to revisit the lessons where we talked about functions in Dart part 1, part 2 and Part 3.
But otherwise, we're going to get back to coding up our Xylophone app and trying to solve the problem
that we had earlier on using our newfound knowledge.
So all of that and more, I'll see you on the next lesson.