All right.
So in the last module, we talked about Dart functions.
We talked about part one of Dart function. And those were the simplest of functions.
They simply package a whole bunch of code together inside some curly braces, so that when you want all
of these instructions to be executed in one go, you can simply call the function by specifying its name.
But it's rather curious how we've been using these parentheses.
We've always left it empty.
What are they for?
Well I like to think of these types of functions as the most basic type of functions.
And there's actually three different levels of functions if you will. If this is the most basic, then
let's call it the vanilla flavor of function.
Now we're coming across the next flavor of functions. And we saw this in the code in the last lesson.
We saw that we could specify an input to our function inside those parentheses, and we could use that
input inside the function.
And this makes our function far more adaptable because we can now get it to do slightly different things
by providing a different input.
So now let's talk about part two of Dart functions. We're leveling up our Dart functions. So coming back
to our little robot,
what if instead of just telling him to go and get milk, we could actually tell him how many bottles of
milk to get? And we would do that by calling the function getMilk and providing an input inbetween
the parentheses.
So if we wanted two bottles, we would write 2,if we want four bottles we would write 4.
So that means that our little robot is a little bit more intelligent
now. We don't have to always get one bottle of milk every single time it goes to the store. Instead of
having a function that looks like this where it's just a whole bunch of instructions packaged together
into a block with a name,
we now have some inputs inside the parentheses. And those inputs can be used inside the block of code
to change the functionality of that block of code.
And now we can use that input inside the function.
For example we could calculate the cost of getting X number of bottles, right?
If they called getMilk and they wanted two bottles of milk,
then if one bottle of milk was £1.5, then that would be £3 for the total cost of
the milk.
Basically, we now have this input or some might call an argument, and this is something that will modify
the functionality of this function. But there's two important parts to this.
One is you have to specify the data type that goes into this function, and you also have to give it a
name so that you can refer to it when you need it inside the curly braces of this block of code.
Now when you call the function, it also looks a little bit different.
We still call it by its name, but inside the parentheses instead of having an empty set up parentheses,
we give it an input.
So let's say we want to get two bottles of milk.
Well then the input now flows in to the function, and it becomes stored inside this name called bottles.
Now the first thing it checks is to make sure that it definitely is getting the right data type as the
input. So it's definitely getting an integer
in this case, and 2 is a whole number,
so that's completely fine.
So when we call getMilk passing in the input of 2, it's almost like we're creating a new variable
that is of data type int.
It has a name of bottle, and it has a value of 2.
So now inside the curly braces of this function, we get to use this new variable, including to calculate
the cost or do whatever we will with that input.
Now it's your part to apply this in practice. Head over to a dartpad and I want you to create a greeting
robot.
Now this greeting robot is pretty simple.
All it needs to do is to print a greeting such as 'Hello Angela' to the console. Notice here, I'm calling
a function called greet, and I'm passing in the input, that's my name, but it could also be anybody else's
name. And this name in here has to be put after a 'hello' or a 'how do you?' do or 'howdy'.
And it has to be printed into the console.
So go ahead and create this great function that achieves this functionality. Pause a video and complete
this challenge.
All right.
So we start out creating our function with the keyword void.
And then we give our function a name.
So in this case it's going to be called greet,
because that's how we said we would be using it.
Now we have a set of parentheses, and inside here,
we specify the argument or the inputs to this function.
Now in this case, we want to be able to pass in a name.
So that later on, we can simply execute the functionality, that's print. And we can say something like hello
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
function will accept is a string, because it's going to be a name,
right?
It'll be Angela or Jack Bauer or whatever it may be.
And then we have to give that a name, and we'll call it personToGreet.
And now, we get to use this personToGreet inside our function.
So every time I say inside, it just means inside the curly braces. And we can use our $ to add
that personToGreet to the end of our hello sentence. And let's cap it off with a semicolon.
And now when I hit run, it will go into main to see what it needs to do.
And we tell it to call the greet function, so it finds this great function. We give it the input or an
argument of Jack Bauer.
So Jack Bauer then goes inside here, and now personToGreet is equal to Jack Bauer.
So now inside this function when it looks to see what it needs to do, it'll print out the words hello
and the personToGreet, which in this case is Jack Bauer.
So it will simply replace it right here.
And so now effectively, we've done the same thing as write print, Hello Jack Bauer.
But it's so much more flexible because we can specify any input and it will simply run it through the
same process.
I hope that makes sense. But you might also realize that there's a bit of a weakness here.
What if we had two things here?
What if we said we had another greeting,
right?
What if, you know, I wanted to be able to
define what I say to this person?
Say I want to be able to say, you know, instead of hello, I might want to say good bye,
right?
So let's use our greeting variable here, and then we've got our personToGreet here. And here, I can simply
write Jackie and I'll say howdy. And I'm going to separate them with a comma.
So now if we click run, you can see that what gets printed is Howdy Jackie.
So if I say how do you do, then our function ends up printing
How do you do
Jackie.
We can use as many inputs as we want but at some point it gets a bit confusing right?
We're calling this function, which one should go where? Is it
this one that should go first
or is it the other one that has to go first?
And notice that this actually matters, the order matters because. If I click run, now it'll instead print
Jackie
How do you do.
Right?
The first part is this greeting, and the second part is this personToGreet. So to minimize confusion,
one of the things that you can do is to add this name into the greet function as an argument name.
So then you would call this function like this.
You would say greet with the greeting,
that is How do you do.
And with the personToGreet being this one.
And when we've been using a lot of functions that came from the Flutter team, this is what we've been
seeing. Now in order for this to work, all you have to do is just add a set of curly braces around your
input parameters. And that allows you to use these names that you've defined in the input when you call
the function as well as when you're inside the function.
And this means that there's less confusion when you're actually trying to trigger the functionality.
So whereas previously we simply had the data type and the name of the input argument,
now we're adding some curly braces around the input arguments where we want to use their name.
And so now what when I call this getMilk function, it won't let me give it an input unless I specified
which argument the input was for.
But it also means I can swap things around now and the order no longer matters.
I can say that the personToGreet is Jackie and the greeting is going to be how do you do.
And now if I hit run, it achieves exactly the same thing. But because I now can associate my data with
the argument,
it's much much clearer what's actually happening with my function.
So now that you've learned all about functions in Dart, let's head back to completing our Xylophone app.