1 00:00:00,390 --> 00:00:06,780 We've managed to get our calculator to work. But in this lesson, I want to talk about something else. 2 00:00:07,200 --> 00:00:15,540 I want to talk about the difference between printing something to the console over here versus returning 3 00:00:15,540 --> 00:00:22,020 something as an output from a function, because a lot of students get very confused at this stage and 4 00:00:22,020 --> 00:00:27,440 they think, well, why don't I just print this results instead of returning it? 5 00:00:27,750 --> 00:00:31,500 What's the actual difference and why would I use return? 6 00:00:32,220 --> 00:00:34,440 That's what I want to address in this lesson. 7 00:00:35,850 --> 00:00:43,050 At the moment, we've got these four functions, and when we call it, we pass over some inputs and 8 00:00:43,050 --> 00:00:48,100 then once we get a hold of the output, we go ahead and print it out into the console. 9 00:00:48,780 --> 00:00:54,660 Now, in the current state of the calculator, it's hard to see the difference between the print and 10 00:00:54,660 --> 00:00:55,280 the return. 11 00:00:55,860 --> 00:00:57,090 But here's a question. 12 00:00:57,510 --> 00:01:04,110 What if we wanted to take the output that comes from calling this function and instead of just storing 13 00:01:04,110 --> 00:01:11,560 it inside a variable and then printing it out, what if we want to pass it as an input to another function? 14 00:01:12,150 --> 00:01:18,960 Let's say that at this point we decided, you know what, we're going to ask them to pick another operation. 15 00:01:19,500 --> 00:01:22,620 And then let's ask them to pick another number. 16 00:01:23,980 --> 00:01:30,820 And finally, let's go ahead and get the calculation function again based on this new operation symbol 17 00:01:30,820 --> 00:01:34,930 that they picked. And then we're going to calculate the new answer 18 00:01:35,320 --> 00:01:42,460 but this time, instead of passing over the calculation function, we're going to pass over the answer 19 00:01:42,460 --> 00:01:44,320 that we got back from this step. 20 00:01:44,740 --> 00:01:47,910 So num1 is now the previous answer 21 00:01:48,490 --> 00:01:53,500 and the second number that's going to be the input is going to be num3. 22 00:01:54,130 --> 00:02:01,780 At this point, when we're calling this calculation function, we're passing over the results or outputs 23 00:02:01,810 --> 00:02:06,000 of this calculation function, and that is going in right here, 24 00:02:06,370 --> 00:02:10,060 and then the third number goes in as the second input. 25 00:02:10,509 --> 00:02:16,230 What basically has happened here is I've taken the output from this function, 26 00:02:16,240 --> 00:02:21,160 so in fact, I could replace this answer with this output, 27 00:02:21,610 --> 00:02:28,120 and I've taken that as the first input into the calculation function and then num3 as the second 28 00:02:28,120 --> 00:02:28,600 input 29 00:02:28,930 --> 00:02:32,350 and then using all of this in order to get the answer. 30 00:02:33,970 --> 00:02:35,830 Let's call this first_answer, 31 00:02:36,710 --> 00:02:41,330 and let's call this second_answer just to differentiate the two answers. 32 00:02:42,390 --> 00:02:48,030 And now I'm going to write a slightly different print statement. In this case, the calculation is done 33 00:02:48,210 --> 00:02:55,530 using the first_answer and then the operation symbol and then the num3 that was inserted at 34 00:02:55,530 --> 00:02:59,490 this point in order to obtain the second_answer. 35 00:03:00,000 --> 00:03:01,680 So now if we give this a run. 36 00:03:06,180 --> 00:03:14,400 Let's do 3 + 2. Now, in this first step of the calculation, we get 5 as the result. 37 00:03:14,820 --> 00:03:19,770 So now if I want to pick another operation, let's I want to multiply 5 by 3. 38 00:03:20,190 --> 00:03:23,190 Then I'm going to take this 5, which is the previous answer, 39 00:03:23,470 --> 00:03:28,710 I'm going to say multiply, and then I'm going to provide the next number, which we'll call 3. 40 00:03:29,190 --> 00:03:36,000 And now it takes the previous answer from the last step and puts that in as the first number in the 41 00:03:36,000 --> 00:03:41,760 calculation, multiplies it by 3 in order to get the final answer. 42 00:03:43,080 --> 00:03:50,520 In this case, what I'm able to do because I have an output from my calculation function up here because 43 00:03:50,520 --> 00:03:57,360 each of these all return their answer, I'm able to take the result from that calculation and plug it 44 00:03:57,360 --> 00:04:06,480 right back into another calculation function using the result of that function call as the input to 45 00:04:06,480 --> 00:04:07,970 another function call. 46 00:04:08,550 --> 00:04:15,270 And I'm only able to do this because I'm using the return statement and because I have outputs from 47 00:04:15,270 --> 00:04:15,990 my functions. 48 00:04:16,320 --> 00:04:19,500 This would not be possible if I was using a print statement. 49 00:04:28,680 --> 00:04:34,800 In the next lesson, we're going to reorganize our code and introduce a while loop so that we can continue 50 00:04:34,800 --> 00:04:37,260 making as many calculations as we want to.