1 00:00:00,490 --> 00:00:03,520 So functions are at the heart of how JavaScript works. 2 00:00:03,520 --> 00:00:06,990 A function is a collection of code that does something as a function, right? 3 00:00:06,990 --> 00:00:09,560 So for example, prompt and alert 4 00:00:09,560 --> 00:00:13,970 are built in JavaScript functions you can use over and over again in your program. 5 00:00:13,970 --> 00:00:16,920 JavaScript also lets you define your own functions. 6 00:00:16,920 --> 00:00:20,480 To create a function, you type the keyword function 7 00:00:20,480 --> 00:00:25,500 followed by the name you want to give the function, for example, myFunction. 8 00:00:25,500 --> 00:00:29,090 Then a set of parentheses and a set of curly braces. 9 00:00:29,090 --> 00:00:32,350 In a function, the Java script you place inside 10 00:00:32,350 --> 00:00:36,080 the curly braces is run whenever the function is activated. 11 00:00:36,080 --> 00:00:39,760 Think of the code inside the braces as a set of instructions that you want to run 12 00:00:39,760 --> 00:00:42,370 each time the function is referenced in your code. 13 00:00:42,370 --> 00:00:46,210 So now I'm going to create a function that contains the same prompt and 14 00:00:46,210 --> 00:00:48,790 alert behavior we wrote earlier in the console. 15 00:00:48,790 --> 00:00:51,900 The function will ask for a user's name and say hello. 16 00:00:51,900 --> 00:00:55,578 So I'll name the function sayHello(). 17 00:00:57,831 --> 00:01:02,484 And between the curly braces, I'll add the response variable and the prompt and 18 00:01:02,484 --> 00:01:04,810 alert commands we wrote earlier. 19 00:01:04,810 --> 00:01:10,800 So I'll type var response and set it equal to prompt, 20 00:01:11,920 --> 00:01:15,820 passing it the message, what is your name? 21 00:01:19,888 --> 00:01:24,450 And below that I'll type alert and 22 00:01:24,450 --> 00:01:32,545 I'll pass alert("Hello", space followed by a plus sign then the response variable. 23 00:01:32,545 --> 00:01:35,106 Plus sign and an exclamation mark. 24 00:01:35,106 --> 00:01:36,898 So this functionality or 25 00:01:36,898 --> 00:01:41,090 set of instructions is now packaged up inside a function. 26 00:01:42,170 --> 00:01:47,040 I'll give this file a save, and nothing visually happens in the browser yet, 27 00:01:47,040 --> 00:01:48,750 even when I refresh. 28 00:01:48,750 --> 00:01:53,340 To activate the programming inside a function, you call the function. 29 00:01:53,340 --> 00:01:56,280 And to call a function and run the code inside it, you write 30 00:01:56,280 --> 00:02:00,470 the name of the function followed by a set of parentheses and a semicolon. 31 00:02:03,450 --> 00:02:07,100 So when you call a function, you're telling the JavaScript interpreter 32 00:02:07,100 --> 00:02:10,940 to jump to that function and run the code inside it. 33 00:02:10,940 --> 00:02:13,940 The syntax for calling a function should look familiar. 34 00:02:13,940 --> 00:02:18,850 Notice how alert and prompt are also calls to the alert and 35 00:02:18,850 --> 00:02:20,870 prompt functions built into JavaScript. 36 00:02:21,870 --> 00:02:26,925 Over in the browser I'll refresh, and now each time the sayHello function is called, 37 00:02:26,925 --> 00:02:29,846 we'll see the dialogue boxes pop up in the browser