We've managed to get our calculator to work. But in this lesson, I want to talk about something else.
I want to talk about the difference between printing something to the console over here versus returning
something as an output from a function, because a lot of students get very confused at this stage and
they think, well, why don't I just print this results instead of returning it?
What's the actual difference and why would I use return?
That's what I want to address in this lesson.
At the moment, we've got these four functions, and when we call it, we pass over some inputs and
then once we get a hold of the output, we go ahead and print it out into the console.
Now, in the current state of the calculator, it's hard to see the difference between the print and
the return.
But here's a question.
What if we wanted to take the output that comes from calling this function and instead of just storing
it inside a variable and then printing it out, what if we want to pass it as an input to another function?
Let's say that at this point we decided, you know what, we're going to ask them to pick another operation.
And then let's ask them to pick another number.
And finally, let's go ahead and get the calculation function again based on this new operation symbol
that they picked. And then we're going to calculate the new answer
but this time, instead of passing over the calculation function, we're going to pass over the answer
that we got back from this step.
So num1 is now the previous answer
and the second number that's going to be the input is going to be num3.
At this point, when we're calling this calculation function, we're passing over the results or outputs
of this calculation function, and that is going in right here,
and then the third number goes in as the second input.
What basically has happened here is I've taken the output from this function,
so in fact, I could replace this answer with this output,
and I've taken that as the first input into the calculation function and then num3 as the second
input
and then using all of this in order to get the answer.
Let's call this first_answer,
and let's call this second_answer just to differentiate the two answers.
And now I'm going to write a slightly different print statement. In this case, the calculation is done
using the first_answer and then the operation symbol and then the num3 that was inserted at
this point in order to obtain the second_answer.
So now if we give this a run.
Let's do 3 + 2. Now, in this first step of the calculation, we get 5 as the result.
So now if I want to pick another operation, let's I want to multiply 5 by 3.
Then I'm going to take this 5, which is the previous answer,
I'm going to say multiply, and then I'm going to provide the next number, which we'll call 3.
And now it takes the previous answer from the last step and puts that in as the first number in the
calculation, multiplies it by 3 in order to get the final answer.
In this case, what I'm able to do because I have an output from my calculation function up here because
each of these all return their answer, I'm able to take the result from that calculation and plug it
right back into another calculation function using the result of that function call as the input to
another function call.
And I'm only able to do this because I'm using the return statement and because I have outputs from
my functions.
This would not be possible if I was using a print statement.
In the next lesson, we're going to reorganize our code and introduce a while loop so that we can continue
making as many calculations as we want to.