1 00:00:00,111 --> 00:00:04,137 You're learning that functions are a way to run several lines of JavaScript code by 2 00:00:04,137 --> 00:00:06,390 issuing just a single command. 3 00:00:06,390 --> 00:00:07,440 When you call a function, 4 00:00:07,440 --> 00:00:11,970 you tell the JavaScript engine to jump to that function and run the code inside it. 5 00:00:11,970 --> 00:00:15,215 Remember back when you said, go to the coffee shop, to your assistant? 6 00:00:15,215 --> 00:00:17,205 They dashed off to the coffee shop. 7 00:00:17,205 --> 00:00:20,135 When your assistant returned, they handed you coffee. 8 00:00:20,135 --> 00:00:23,425 Well, functions can also give something back when they finish. 9 00:00:23,425 --> 00:00:27,025 This is called, returning a value, and it's common when working with functions. 10 00:00:28,765 --> 00:00:32,105 For example, let's say you needed a way to get the current time so 11 00:00:32,105 --> 00:00:34,240 that you can display it on a web page. 12 00:00:34,240 --> 00:00:36,540 You could create a function that gets the current time and 13 00:00:36,540 --> 00:00:38,660 actually writes it to the page. 14 00:00:38,660 --> 00:00:42,720 Better yet, you could create a function that just gets the time and 15 00:00:42,720 --> 00:00:43,770 gives it back to you. 16 00:00:43,770 --> 00:00:46,780 That way you can not only write the return value to the page, but 17 00:00:46,780 --> 00:00:49,290 you could also use the return value in other ways, 18 00:00:49,290 --> 00:00:53,580 like displaying it in an alert box, or printing it to the console. 19 00:00:53,580 --> 00:00:56,830 In other words, creating a function that just does one thing, 20 00:00:56,830 --> 00:00:59,740 returns the current time, makes the function flexible enough 21 00:00:59,740 --> 00:01:02,050 that you can use it in many different situations. 22 00:01:03,540 --> 00:01:07,760 To return a value from a function, you use the return keyword. 23 00:01:07,760 --> 00:01:10,890 This creates what's called a return statement. 24 00:01:10,890 --> 00:01:12,250 When the JavaScript engine, or 25 00:01:12,250 --> 00:01:14,760 interpreter, encounters the return keyword, 26 00:01:14,760 --> 00:01:18,250 it immediately jumps out of the current function and provides the value to return. 27 00:01:19,310 --> 00:01:23,570 The value that's returned by the function can then be used in your program. 28 00:01:23,570 --> 00:01:28,020 For example, in this case, the return value is given to the alert function. 29 00:01:28,020 --> 00:01:31,930 So the message, your coffee is on its way, appears in an alert dialog box. 30 00:01:31,930 --> 00:01:34,940 But you could use this return value in many different ways. 31 00:01:34,940 --> 00:01:37,401 Print it to the console, or even save it in a variable. 32 00:01:39,200 --> 00:01:41,850 Let's look at the alertRandom function we created earlier and 33 00:01:41,850 --> 00:01:43,750 see how we can improve it. 34 00:01:43,750 --> 00:01:47,090 Generating a random number from one to six is useful. 35 00:01:47,090 --> 00:01:49,890 You can use it to build a dice-rolling game, for example. 36 00:01:49,890 --> 00:01:52,260 But the function we created is limited. 37 00:01:52,260 --> 00:01:56,360 It generates a random number but always displays it in an alert box. 38 00:01:56,360 --> 00:01:58,566 What if we wanted to show the random number on the page, 39 00:01:58,566 --> 00:02:01,380 appear on the console, or store it in a variable? 40 00:02:01,380 --> 00:02:05,040 We can make this function more useful by returning the random number only. 41 00:02:05,040 --> 00:02:07,430 That way we could use it any way we wanted to. 42 00:02:07,430 --> 00:02:10,990 I'll show you what I mean by first removing the alert method 43 00:02:10,990 --> 00:02:12,510 from the function. 44 00:02:12,510 --> 00:02:16,536 Next, I'll add a return statement to return a value from a function. 45 00:02:16,536 --> 00:02:18,790 You use the return keyword. 46 00:02:18,790 --> 00:02:20,150 And what do we want to return? 47 00:02:20,150 --> 00:02:23,270 Well, the value stored in randomNumber. 48 00:02:23,270 --> 00:02:27,020 So now the function name alertRandom doesn't make a whole lot of sense, 49 00:02:27,020 --> 00:02:29,800 because the function no longer pops up an alert. 50 00:02:29,800 --> 00:02:33,892 So I'll change it to getRandomNumber, because that's what it's now doing. 51 00:02:37,190 --> 00:02:42,480 So now I can use this function anywhere I want to get a number from one to six. 52 00:02:42,480 --> 00:02:45,529 For example, in an alert dialogue like this. 53 00:02:58,020 --> 00:03:03,345 I can also print it to the console with the console.log method. 54 00:03:11,337 --> 00:03:15,520 I can even store the function in a variable that I can use later. 55 00:03:15,520 --> 00:03:22,910 For example, const dieRoll = a call to getRandomNumber. 56 00:03:22,910 --> 00:03:23,410 And that's right, 57 00:03:23,410 --> 00:03:27,780 a function call itself can be a value that gets stored in a variable. 58 00:03:29,220 --> 00:03:33,210 Creating functions that only return a value is common in programming. 59 00:03:33,210 --> 00:03:37,040 For example, you might create a function that computes the total cost of an item, 60 00:03:37,040 --> 00:03:38,750 including tax and shipping. 61 00:03:38,750 --> 00:03:42,810 The function would return that total cost so that you can use it in other ways, 62 00:03:42,810 --> 00:03:47,170 like displayed on the web page, or use it for more calculations. 63 00:03:47,170 --> 00:03:49,660 Lastly, functions always return a value, 64 00:03:49,660 --> 00:03:51,940 even when they don't have a return statement. 65 00:03:51,940 --> 00:03:56,950 If you don't specify a return value, the function returns undefined. 66 00:03:56,950 --> 00:03:59,047 For example, in the console, 67 00:03:59,047 --> 00:04:03,740 I’ll paste the alertRandom function we first wrote, then call it. 68 00:04:07,103 --> 00:04:10,460 When I run this code, the browser pops up the alert, and 69 00:04:10,460 --> 00:04:13,730 notice that undefined displays in the console. 70 00:04:13,730 --> 00:04:16,320 Undefined is one of JavaScript's built-in values. 71 00:04:16,320 --> 00:04:20,390 When returned by a function, it means that there's no return value specified. 72 00:04:22,390 --> 00:04:25,770 Now, I'll call the getRandomNumber function here in the console. 73 00:04:27,140 --> 00:04:29,688 And this time, the console inputs a random number value.