So in the last lesson, we came across this error here where it tells us that this expression here, our built
Key function, has a type of void and therefore cannot be used here.
So in order to solve this conundrum, we have to understand a little bit more about how functions work.
So here I've deleted everything in my dartpad
other than the main function. And I'm going to create a new function. I'm going to call it myFunction,
and it's not going to have any input, so I'm going to leave the parentheses here empty, but it will do
something. In between the curly braces, I'm going to create a new variable that's going to hold values
of the type double, and it's gonna be called pi. So pi is gonna be its name. And I'm gonna set it to equal
3.14159, the value of the mathematical pi. And in my function, all that's gonna happen is
this value pi, it's gonna be multiplied by two.
Now I've got my function created, we know that the main function is the entry point of all of our Dart
files.
So whenever we hit run, it's going to look inside here to see what should happen. Inside here,
I'm going to call that function I just created, which is called myFunction, and it's gonna have some
empty parentheses because it doesn't take inputs.
And now if I click run, you can see that pretty much nothing happens.
Now firstly because I'm not really printing anything to the console, and all of this just happens behind
the scenes without us being able to see what happens.
But rest assured, this does get called and this in fact, does get carried out and we can make sure of this
by simply adding a print statement here.
Let's print and say that, 'my function got called'. We know that if this gets printed into the console, then
this must have been triggered successfully from our main function.
And indeed it does.
So we can be sure that these instructions do in fact get carried out.
But how can we get hold of this value, this pi * 2? Kind of like the result of this function,
right?
How can we get hold of it and bring it into our main function?
What if we wanted to get something out of this function so that once we run this function, it outputs
the value that it's calculated and then we can store it inside a variable? Let's call it maybe result.
And then we could maybe just print the result from our main function.
So in this case, we're basically trying to do something inside my function.
And then once the results been calculated, we want this to be transported back into our main function
so that we can use it here to do other things such as printing it.
Now at the moment, this doesn't work.
And when I click run, you can see that the error that I get, both from the Dart error checker as well
as in the console,
it tells me that the expression here has a type of void and therefore can't be used.
And it's pointing to this part,
myFunction.
Now if this sounds familiar, it's because it's the same error that we got over here.
So what does this mean?
Well it means that this function currently doesn't actually output anything. It outputs void, which in
programming lingo, basically means that there is no output.
Now what if we wanted it to give us a output?
Well we would change that from void to the data type of the output.
So let's say in this case, we times pi by 2.
It's still going to be a number with decimal places right?
So the data type of the output is still going to be a double.
And the final thing we need to do, is we need to mark the thing that we want to come out of the function
with the keyword return.
So now that we've updated our code, our error goes away
and when I click run, we do in fact get 6.28318 printed over here.
And remember the print statement is inside our main function.
So in order for it to get a hold of this result, it had to call my function.
So we had to run these two lines of code and then the result of pi * 2 gets outputed from my function,
transported back in here and assigned to that variable result.
And this is how we're able to print it into the console. So this is how we would solve the problem that
we have with our Xylophone app.
But how exactly do functions that have outputs work?
Let's explore this in a little bit more detail. Previously when we talked about the second type of function,
so functions that can take inputs,
we saw that we could put a data type inside the parentheses and give the input a name, so that when we
call getMilk we could provide a value here and that value will then be used inside the function or
inside the curly braces.
So in this case for example, our getMilk function takes a value, which is the amount of money that the
robot got given to buy a single bottle of milk,
and that value gets used to calculate the amount of change that the robot should get back. Which is the
amount of money that it got given minus the price of a single bottle of milk.
So money minus two.
And we end up with the change.
Now how can we get this change out of this function?
Well in that case, we have to use the third type of functions, which are functions that can have a output.
And in order for our function to have an output, we have to change the way that we created a little bit.
Instead of having void, which marks the function as having no outputs, we change it to the data type that
we expect to come out of the function, which in this case can be an integer.
And we also specify what should come out of the function by using a keyword, which is return.
So in this case we're saying the output of this function is change.
So then if we gave our robot five coins and told it to buy some milk, it's the equivalent of calling
our getMilk function passing in the number five, robot buys us a bottle of milk.
And if the bottle milk costs 2 coins, then it gives us 3 coins back as the change.
And so this expression over here, where it says getMilk passing in an input of 5, because it's able
to have an output, once this line of code runs it will be equivalent to 3.
So we could assign the output of getMilk to a variable called result.
And in this case result would be equal to the output which is 3.
So the result is equal to 3 because getMilk, starting with 5 coins is equal to 3. So
in this case when we're creating the function, this is what it looks like.
And when we're calling the function,
this is what it would look like.
This function has both an input, which is specified in between the parentheses, and also it can have an
output which is specified after the keyword return.
That keyword return marks whatever comes after it as the output of the function, and the int keyword
at the beginning of our function tells us what is going to be the data type of the output of this function.
So when we call the function, we can simply call it by its name passing in any inputs if it takes any.
And once that gets carried out, the result or in this case money - 2, gets assigned to this variable.
This is how the values would move through the function. If we were calling getMilk passing in the value
5, 5 goes into the input and it gets used inside the function.
So we end up with 5 - 2 as the return which is equal to 3 of course. And 3 is definitely
a whole number
so it fits the integer data type that we specified when we created the function.
So that becomes the result of getMilk passing in an input of 5, and it replaces the right hand side
of the equation.
So now we have a variable called change which is equal to 3.
So at this stage, we've seen all three types of functions.
We've seen the simplest type,
the vanilla function, which basically just packages a whole set of instructions together into a function.
And whenever we call the function, we specify its name and a set of parentheses,
then it will go into the curly braces of where this was created and execute all the instructions in
that line by line.
This is kind of just like a machine that does something, right? It does some calculations or it does some
processing, but essentially there's no input and no output.
Now the next type of function gets a little bit more exciting,
this is our chocolate flavor function. And it now can take inputs and it takes those in between our parentheses.
We have to specify a data type for the input so that when we call the function, we can give it a piece
of data that matches that data type, and that number or that input gets used inside the function to do
something or make this function more customized.
And if this was a machine, then it's kind of like passing in a value into the machine so that it processes
the data a little bit differently and you can end up with some custom functionality. So that if the input
is different, then the functionality will also be a little bit different each time.
Now this leads us to our third type of functions and the most advanced type of functions that we've
seen so far. And this is where we can have both an input which goes into the parentheses as well as an
output which we specify in the beginning, and also we specify inside the function to say what should
be the output.
And this is equivalent to a fully fledged machine where not only can you provide an input, but you can
also get something that comes out of the machine, as an output.