So previously we've talked about all sorts of different functions with inputs and outputs.
But right up here, we've got a function but the syntax looks a little bit unfamiliar.
What is this arrow anyways?
Previously we said that this line of code is equivalent to basically writing something like this.
We have a function that's code in main, and inside this function, we carry out a single instruction which
is to run our app. And the app that we want to run is our Xylophone app right here.
And once that run app is done, then we have our semicolon to cap off the line and this is exactly the
same as what we had before.
But what is that arrow and what does it do?
Well the arrow which we create using a equals sign and a right angle bracket, in Dart, is known as the
arrow syntax. And you might hear other people referring to it as a fat arrow to distinguish it from a
slim arrow, I guess.
But the arrow syntax is used when the function body,
so the part that's inside the curly braces right after the name of the function, with this part is only
a single line of code.
Heading back to a dartpad so I can show you how this works.
Let's say that we have our add function which returns an integer. And all that it does is it adds the
numbers 5 + 2. And then it returns 5 + 2 as the output which is going to be 7 and
inside our main function, all that we do is we created integer variable which is called result, and we
set it equal to the output of our add function.
Now clearly at this point, if we print our result, we'll just get 7 printed in the console right
here. 5 + 2 = 7.
That becomes the result over here.
And so result = 7, and when we print this line, this is what we get.
Now we can also represent this function, because it only has a single line of instructions,
we can also use our arrow syntax. So we can write int add and after the parentheses, we can add our arrow
and we can omit the word return, but simply give the expression or what it needs to do.
So this is exactly the same to the computer as this.
And if I comment this out and I click run again, you'll see that when it calls add it's going to use this
version and it does exactly the same thing.
So that arrow syntax is equivalent to having the curly braces around whatever comes after.
And it also is equivalent to having that return keyword in front of this line of syntax.
Now remember, the arrow syntax only works if you have a single line of instructions.
So for example, you can't for example have a print statement here as well. Print
'I got called'.
So now this function has two lines, and we can't combine that into the end of our arrow syntax. So we
can't for example put it here and try to run both lines.
If I go ahead and comment this out and click run, you can see that I get all sorts of errors.
So I'm only allowed a single line expression. But even if I'm using the arrow syntax, I can still have
inputs inside here.
So for example, if I didn't want to add 5 to 2 every single time, which is a bit of a useless function
if you think about it, let's try and add our n1 to our n2 and we return simply n1 + n2.
Now in the arrow syntax version, we would represent this in much the same way inside the parentheses
after the word add, we add our inputs in exactly the same way.
So I'm just going to copy and paste it in there. And then after the arrow, we place our single line of
instructions, which is basically just n1 + n2.
So this also works and this is exactly the same as this line of code.
So if we added our numbers let's say let's add 3 to 11, and we hit run.
You can see that our function, using the arrow syntax, returns the number 14 which gets printed in here.
So now if we head back to Android Studio, we can see that using the same principles, we can get rid of
are curly braces.
And even though in this case, this function doesn't actually return anything because it has a void data
type,
we can still use that arrow syntax to say that this is the body of this function.
This is the single line of instructions which need to be carried out when this function gets called,
which is simply to run the app
that is our Xylophone app.
So next time when you see this arrow syntax, you won't be confused as to what it does anymore.
And after this module, hopefully you now have a good grasp of how packages work in Flutter, how we can import
other people's code into our own projects and leverage the code that they've already written to give
our apps more functionality without having to write a lot more code.
We've also explored how functions work in all three flavors in Dart.
And we've built out a music app which is created using one of these functions that we learnt about. So
that's all from me and I'll see you on the next module.