0 1 00:00:00,240 --> 00:00:06,180 Now in the last lesson we looked at the chocolate flavor of functions, which are functions that are able 1 2 00:00:06,180 --> 00:00:13,800 to take an input and use that input inside the function to do some calculation or to use it as a part 2 3 00:00:13,800 --> 00:00:14,580 of the code. 3 4 00:00:14,580 --> 00:00:20,670 Now in this lesson, I want to show you an even more complex and also more useful type of function, which 4 5 00:00:20,670 --> 00:00:24,020 are functions that are able to give an output. 5 6 00:00:24,030 --> 00:00:30,210 So for example, taking the previous example where we're able to take money as an input to the function 6 7 00:00:30,630 --> 00:00:36,990 and use it to calculate the number of bottles of milk that we can buy, at the end of the code we can 7 8 00:00:36,990 --> 00:00:44,830 use the keyword return to specify that this function, in addition to being able to take inputs, 8 9 00:00:44,910 --> 00:00:48,040 it can also give an output. 9 10 00:00:48,480 --> 00:00:53,800 And in our case we're giving the output of money modulo 1.5. 10 11 00:00:53,820 --> 00:00:58,210 So remember that 1.5 is the cost of a bottle of milk. 11 12 00:00:58,240 --> 00:01:06,120 So money modulo 1.5 means that we're getting the remainder of how much we've spent to buy the milk. 12 13 00:01:06,120 --> 00:01:13,260 So, if you remember, the modulo works like this. Whereas 9 divide by 6, 6 only goes into 9 once. 13 14 00:01:13,320 --> 00:01:16,240 So we get a remainder of three. 14 15 00:01:16,260 --> 00:01:23,770 So in this case the variable e is equal to 3, because the modulo gives you the remainder of the division. 15 16 00:01:23,790 --> 00:01:31,260 So in this function if we gave an input of $4 and each bottle of milk cost $1.5 then the robot will 16 17 00:01:31,260 --> 00:01:38,880 buy 2 bottles of milk which will cost $3 and it'll give us $1 change. And that $1 of 17 18 00:01:38,880 --> 00:01:45,920 change is the output from this function which we can bind to a variable called change. 18 19 00:01:45,930 --> 00:01:52,370 So, for example, we can say variable change is equal to the outcome of the function 19 20 00:01:52,410 --> 00:01:54,450 getMilk with an input of 4. 20 21 00:01:54,510 --> 00:02:00,950 And change will equal 1, because remember each bottle of milk costs 1.5. 21 22 00:02:01,080 --> 00:02:08,940 So $4 only gets you two bottles of milk which costs $3 and that gives you a remainder of 1. 22 23 00:02:08,940 --> 00:02:13,530 So now the output of this entire function is equal to 1, 23 24 00:02:13,530 --> 00:02:19,180 and that gets assigned as the value to the variable change. That might take a little bit to sit in. 24 25 00:02:19,260 --> 00:02:23,710 But let's just review creating the function, this third type of function. 25 26 00:02:23,730 --> 00:02:28,740 So the important things are we have the function keyword that specifies we're creating a new function. 26 27 00:02:28,890 --> 00:02:34,860 We have to give the function a name straight afterwards and then inside the curly braces we specify 27 28 00:02:34,920 --> 00:02:39,470 whether or not if we have an input that this function is going to use. 28 29 00:02:39,600 --> 00:02:46,290 And in order to get an output out of the function we have to use that keyword return, and whatever gets 29 30 00:02:46,290 --> 00:02:51,790 returned in the function becomes the right hand side of this function call. 30 31 00:02:51,810 --> 00:02:58,500 So when we call a function that has an output we can use the output and we can assign it to a variable, 31 32 00:02:58,530 --> 00:03:01,580 for example in this case the variable change. 32 33 00:03:01,650 --> 00:03:08,460 So if we didn't have this line where we have a return statement or specify that our function has an output, 33 34 00:03:08,700 --> 00:03:14,940 then calling getMilk only executes the function without getting an output back. 34 35 00:03:14,940 --> 00:03:17,940 So this is the most complex of all three functions. 35 36 00:03:18,130 --> 00:03:22,920 And this is the last flavor that we're looking at, which is the strawberry flavor, which incidentally 36 37 00:03:22,920 --> 00:03:26,880 is also the flavor that I never touched because it tastes awful. 37 38 00:03:26,880 --> 00:03:33,470 But our third function doesn't have to be awful as long as we understand how the return works. 38 39 00:03:33,600 --> 00:03:36,740 So let's take a look at this in practice. 39 40 00:03:36,760 --> 00:03:45,120 So currently we've got a function that takes an input called money, and it uses that input to calculate 40 41 00:03:45,150 --> 00:03:49,210 how many bottles of milk we're able to buy using that money. 41 42 00:03:49,260 --> 00:03:58,380 But at the end of the function, just before we hit the closing brace, we can add a return keyword and 42 43 00:03:58,380 --> 00:04:06,990 specify that we want this function to output a number and the number is going to be money modulo 1.5 43 44 00:04:07,140 --> 00:04:10,420 which is the cost of a bottle of milk. 44 45 00:04:10,830 --> 00:04:15,590 So this will give us the remainder of this division. 45 46 00:04:15,630 --> 00:04:23,950 So that means that if we give say an input of 3, 3 divided by 1.5 has a remainder of 0. 46 47 00:04:24,060 --> 00:04:32,220 So we'll end up returning 0. But if we gave it a value 4, then we will have a remainder of 1, and that 47 48 00:04:32,220 --> 00:04:36,280 number 1 becomes the output of this function call. 48 49 00:04:36,390 --> 00:04:45,540 So we can capture it and save it inside a variable called change, for example, and I can log the value 49 50 00:04:45,570 --> 00:04:50,950 of change by writing console.log(change). 50 51 00:04:50,970 --> 00:04:58,320 So now if I hit run you can see that all of those lines of code get executed and it shows me that I'm 51 52 00:04:58,320 --> 00:05:04,800 buying two bottles of milk but it also shows me that I'm getting a change of one and that comes from 52 53 00:05:04,800 --> 00:05:06,090 this return statement. 53 54 00:05:06,180 --> 00:05:14,510 So this is quite useful because it allows us to use a number of functions inside other functions. 54 55 00:05:14,550 --> 00:05:24,960 So for example if, instead of messing up my bunch of instructions, I took this part where I calculate 55 56 00:05:24,960 --> 00:05:30,450 the number of bottles of milk I get based on money into its own function. 56 57 00:05:30,450 --> 00:05:38,550 So, for example, let’s create a function called calcBottles, and it takes an input in the form of startingMoney 57 58 00:05:39,030 --> 00:05:43,210 and also costPerBottle. 58 59 00:05:43,230 --> 00:05:52,440 So we can now say that the number of bottles is equal to this input called startingMoney divided by 59 60 00:05:52,680 --> 00:05:54,910 the costPerBottle, 60 61 00:05:55,230 --> 00:05:55,560 right? 61 62 00:05:55,560 --> 00:06:00,750 This is always going to be the same calculation that you need to perform no matter which scenario. 62 63 00:06:00,750 --> 00:06:04,980 So you could have a starting money amount of $5 $10 $20. 63 64 00:06:05,010 --> 00:06:05,920 It doesn't matter. 64 65 00:06:05,920 --> 00:06:11,610 And if you divide that by the cost per bottle of milk then you will end up with the number of bottles. 65 66 00:06:11,610 --> 00:06:15,610 So now, how can we use this function inside here? 66 67 00:06:15,690 --> 00:06:23,310 Well, if we give this function a return statement, and we say that the output of this function is the 67 68 00:06:23,310 --> 00:06:35,380 number of bottles, then up here we can write console.log “buy “ + calculate bottles using the amount 68 69 00:06:35,380 --> 00:06:41,680 of money we got as an input over here as the first parameter, which is startingMoney, 69 70 00:06:42,050 --> 00:06:50,470 and the second input, 1.5 as the second parameter, which is the cost per bottle, and now, once our code 70 71 00:06:50,470 --> 00:06:58,060 gets to this line, which is line number 12, then it’ll go and find where this function was created, calcBottles, 71 72 00:06:58,450 --> 00:06:59,790 which is down here of course, 72 73 00:06:59,830 --> 00:07:07,360 it will place the value of money into startingMoney, 1.5 into costPerBottle, and it will calculate 73 74 00:07:07,690 --> 00:07:09,060 the number of bottles. 74 75 00:07:09,400 --> 00:07:17,470 And once it gets to this return statement, that value in numberOfBottles will replace this part of 75 76 00:07:17,470 --> 00:07:21,220 the code because it is the output of the function. 76 77 00:07:21,220 --> 00:07:31,810 So now if I call my first function getMilk and I specify an input of say $5, then that $5 is going to 77 78 00:07:31,810 --> 00:07:39,100 go into this parameter called money and then it's going to move over here in order to use it to calculate 78 79 00:07:39,100 --> 00:07:41,290 the number of bottles we’ll get back. 79 80 00:07:41,290 --> 00:07:50,110 So if I hit run you'll see that all of the instructions are executed and it also tells me that I'm able 80 81 00:07:50,110 --> 00:07:56,080 to buy 4 bottles of milk and have 0.5 as the change. 81 82 00:07:56,080 --> 00:07:58,690 Now what if we did the same thing down here? 82 83 00:07:58,750 --> 00:08:05,110 What if, instead of doing the calculation in here, we created another function, and we called this function 83 84 00:08:05,590 --> 00:08:15,630 calcChange, and the inputs that it's going to take is startingAmount and also costPerBottle. 84 85 00:08:15,700 --> 00:08:23,890 And inside this function I'm going to calculate change, which is going to be equal to startingAmount 85 86 00:08:24,010 --> 00:08:26,330 modulo costPerBottle. 86 87 00:08:26,560 --> 00:08:31,430 And that is what I'm going to return as the output of this function. 87 88 00:08:31,840 --> 00:08:41,050 So now, instead of returning this calculation, I can simply return calcChange, and the first input is 88 89 00:08:41,050 --> 00:08:42,800 going to be money, 89 90 00:08:42,970 --> 00:08:51,610 and the second input, costPerBottle, is going to be 1.5, and that becomes the output of this function, 90 91 00:08:51,670 --> 00:08:53,310 which is getMilk. 91 92 00:08:53,380 --> 00:09:01,000 So instead of just calling getMilk here, I can also say console.log, call this function getMilk, 92 93 00:09:01,240 --> 00:09:03,550 and we're going to add it to a string, 93 94 00:09:03,580 --> 00:09:07,080 so we're going to say “Hello master, 94 95 00:09:07,270 --> 00:09:09,740 here is your “ 95 96 00:09:09,760 --> 00:09:13,590 whichever amount that you get + “ change.”. 96 97 00:09:13,840 --> 00:09:20,350 So now if I run the code you can see that it goes through all of the commands, buys 3 bottles of 97 98 00:09:20,350 --> 00:09:21,110 milk, 98 99 00:09:21,310 --> 00:09:26,320 and finally we get “Hello master, here is your $0.5 of change.”. 99 100 00:09:26,360 --> 00:09:36,450 So as you can see what we're doing here is we're using the outputs of functions in order to use it inside 100 101 00:09:36,540 --> 00:09:38,760 other lines of code. 101 102 00:09:38,880 --> 00:09:43,600 So now, say if the cost per bottle of milk changed, 102 103 00:09:43,620 --> 00:09:44,180 right? 103 104 00:09:44,300 --> 00:09:48,840 Say it now costs $2.5 instead of 1.5. 104 105 00:09:48,870 --> 00:09:51,570 Then we don't have to change the calculation. 105 106 00:09:51,600 --> 00:09:57,150 And in fact we can make this even more dry by reducing the repetition inside the function. 106 107 00:09:57,360 --> 00:10:06,630 So as an input we can say getMilk, the amount of money as an input and also the cost per bottle as an 107 108 00:10:06,630 --> 00:10:07,380 input, 108 109 00:10:07,860 --> 00:10:09,870 and we can replace that hit. 109 110 00:10:10,020 --> 00:10:16,920 So now, when we call getMilk, we have to say the amount of money we got given is 5 and the cost per 110 111 00:10:16,920 --> 00:10:19,230 bottle is say 1.5. 111 112 00:10:19,290 --> 00:10:26,160 So this will give us 3 bottles of milk and 0.5 change, but say tomorrow the master gives 112 113 00:10:26,160 --> 00:10:28,440 us $10 to buy milk, 113 114 00:10:28,710 --> 00:10:36,510 and the cost of milk has gone up to $3, and now we're able to only buy 3 bottles of milk but we get 114 115 00:10:36,510 --> 00:10:37,910 $1 change. 115 116 00:10:37,980 --> 00:10:44,550 You will find yourself using all three types of functions in a variety of scenarios depending on what 116 117 00:10:44,550 --> 00:10:46,370 it is that you're trying to do. 117 118 00:10:46,390 --> 00:10:49,150 There's a lot of new information that you've learned. 118 119 00:10:49,200 --> 00:10:56,310 So it's really really crucial that you try to understand this information by completing some exercises 119 120 00:10:56,610 --> 00:10:58,980 and trying to write the code yourself. 120 121 00:10:58,980 --> 00:11:05,400 So in the next lesson I have an interactive challenge for you where I want you to write some code and 121 122 00:11:05,400 --> 00:11:10,790 we'll use some software on our site to test your code and see if you got it right. 122 123 00:11:11,220 --> 00:11:15,300 So for all of that and more exciting stuff, I'll see you on the next lesson.