1 00:00:07,970 --> 00:00:14,280 Welcome back. A function can be defined so that it does different things, 2 00:00:14,280 --> 00:00:17,625 depending on parameter values that are passed to it. 3 00:00:17,625 --> 00:00:22,035 For example, here's a definition of the function Hello2, 4 00:00:22,035 --> 00:00:25,440 the only difference in this function versus 5 00:00:25,440 --> 00:00:31,990 our previous Hello is that we have another variable name inside the parentheses. 6 00:00:33,260 --> 00:00:39,120 We've called it S, that's called a formal parameter name, 7 00:00:39,120 --> 00:00:41,685 any variable name inside those parentheses. 8 00:00:41,685 --> 00:00:45,940 So, sometimes we call it a formal parameter or a parameter name. 9 00:00:45,940 --> 00:00:49,350 Then, when we invoke the function, 10 00:00:49,350 --> 00:00:52,065 and on line five, 11 00:00:52,065 --> 00:00:54,390 we'll pass some string into it. 12 00:00:54,390 --> 00:00:58,185 In this case, we're going to pass the string Iman. 13 00:00:58,185 --> 00:01:01,070 By the way that's Iman [inaudible] he's 14 00:01:01,070 --> 00:01:05,485 the guy who developed a practice tool that I hope you are using a lot. 15 00:01:05,485 --> 00:01:07,905 When we do that invocation, 16 00:01:07,905 --> 00:01:13,780 the variable s gets the value Iman at the beginning of the execution of the function. 17 00:01:13,780 --> 00:01:16,930 It's like having a behind the scenes assignment statement. 18 00:01:16,930 --> 00:01:26,335 It's as if we have a line of code that says s equals Iman. 19 00:01:26,335 --> 00:01:29,880 Then, in the rest of the execution, 20 00:01:29,880 --> 00:01:35,450 we'll be able to refer to the variable s. So, 21 00:01:35,450 --> 00:01:38,950 let's actually do this using CodeLens. 22 00:01:38,950 --> 00:01:44,830 We first create the function and assign it to the name Hello2, 23 00:01:44,830 --> 00:01:47,460 and then we get to line five, 24 00:01:47,460 --> 00:01:50,355 we're going to invoke the function. 25 00:01:50,355 --> 00:01:54,069 But behind the scenes, there was this assignment, 26 00:01:55,760 --> 00:01:59,950 s is now bound to Iman. 27 00:02:00,200 --> 00:02:05,070 So that when we print Hello plus s, 28 00:02:05,070 --> 00:02:09,400 we get Hello Iman in the output window, 29 00:02:10,130 --> 00:02:16,410 and then it says "Glad to meet you" and we're done with that invocation. 30 00:02:16,410 --> 00:02:18,945 The second time we invoke it, 31 00:02:18,945 --> 00:02:23,115 we're going to get s bound to Jackie, 32 00:02:23,115 --> 00:02:29,010 because Jackie is the value that is passed in. 33 00:02:29,930 --> 00:02:33,505 So now, s is bound to Jackie, 34 00:02:33,505 --> 00:02:40,620 and we'll get Jackie is greeting appearing in the output window. "Hello Jackie." 35 00:02:42,590 --> 00:02:47,350 Often we'll refer to the parameter values as inputs. 36 00:02:47,350 --> 00:02:50,325 Don't confuse that with the input function. 37 00:02:50,325 --> 00:02:54,110 Remember, the input function asks the user to type in a value. 38 00:02:54,110 --> 00:03:00,365 Here, we're talking about a value that is passed into a function as an input to it. 39 00:03:00,365 --> 00:03:04,680 So, this Jackie, it's an input to 40 00:03:04,680 --> 00:03:11,550 the Hello2 function or sometimes we'll call it an input parameter or a parameter value. 41 00:03:12,040 --> 00:03:16,555 A function can take more than one input parameter. 42 00:03:16,555 --> 00:03:21,930 Here, the function hello3 has two formal parameters; 43 00:03:21,930 --> 00:03:31,935 s and n. We're going to refer to both of those parameters as we refer to on line two, 44 00:03:31,935 --> 00:03:35,185 and we refer to on line three. 45 00:03:35,185 --> 00:03:38,600 Remember that star when applied to 46 00:03:38,600 --> 00:03:43,120 strings means to repeatedly concatenate the string together. 47 00:03:43,120 --> 00:03:48,660 So, the greeting is going to be something like "Hello Wei" or "Hello 48 00:03:48,660 --> 00:03:54,440 Kitty" and we're going to concatenate that together to itself a bunch of time, 49 00:03:54,440 --> 00:03:58,080 so it just repeats that string n times. 50 00:03:58,480 --> 00:04:01,740 Let's step through that code. 51 00:04:01,930 --> 00:04:05,925 We create the string, 52 00:04:05,925 --> 00:04:09,840 or we create the function rather, and then we call it. 53 00:04:09,840 --> 00:04:12,190 The first time we call it, 54 00:04:12,350 --> 00:04:19,210 we are passing in two parameter values, Wei and four. 55 00:04:19,280 --> 00:04:23,510 The very first parameter value automatically 56 00:04:23,510 --> 00:04:27,475 gets matched with the first formal parameter name. 57 00:04:27,475 --> 00:04:31,020 So, we get s bound to Wei. 58 00:04:31,020 --> 00:04:34,955 The second value goes to the second parameter name. 59 00:04:34,955 --> 00:04:37,070 So, n gets the value four. 60 00:04:37,070 --> 00:04:40,360 We call this positional parameter passing. 61 00:04:40,360 --> 00:04:45,740 The first parameter value goes with the first parameter name. 62 00:04:48,350 --> 00:04:52,420 When we execute on line two, 63 00:04:53,120 --> 00:04:56,850 we'll get a value for greeting, 64 00:04:56,850 --> 00:05:00,480 that's "Hello Wei" and on line three, 65 00:05:00,480 --> 00:05:04,290 we will print that out a bunch of times, 66 00:05:04,290 --> 00:05:09,750 and you can see down here we have said Hello Wei four times. 67 00:05:13,460 --> 00:05:16,620 The next time we execute this, 68 00:05:16,620 --> 00:05:21,555 we have s is bound to an empty string and n is one, 69 00:05:21,555 --> 00:05:26,790 so our greeting is just going to be Hello with a couple of spaces. 70 00:05:26,790 --> 00:05:30,140 We're going to print that out one time and it looks 71 00:05:30,140 --> 00:05:33,610 just like Hello on the output because we can't see the spaces. 72 00:05:33,610 --> 00:05:36,165 The last time we invoke this, 73 00:05:36,165 --> 00:05:39,360 we've got Kitty and 11 is our two parameter values, 74 00:05:39,360 --> 00:05:44,130 two inputs and so we get Hello Kitty, 75 00:05:44,130 --> 00:05:48,010 Hello Kitty, Hello Kitty, 11 times. 76 00:05:48,320 --> 00:05:55,260 In summary, formal parameters sometimes called parameter names or input parameters, 77 00:05:55,260 --> 00:06:00,455 those are inside the parentheses in the function definitions. 78 00:06:00,455 --> 00:06:04,720 So, s and n are formal parameters. 79 00:06:04,720 --> 00:06:07,790 Then, we have parameter values or 80 00:06:07,790 --> 00:06:11,450 sometimes they're called arguments or actual parameters. 81 00:06:11,450 --> 00:06:15,380 Those go inside the parenthesis on the function invocation. 82 00:06:15,380 --> 00:06:18,470 They get matched up positionally, 83 00:06:18,470 --> 00:06:21,190 first parameter value with first parameter name, 84 00:06:21,190 --> 00:06:24,445 second parameter value with second parameter name. 85 00:06:24,445 --> 00:06:28,670 The values are bound to those parameter names at the beginning of 86 00:06:28,670 --> 00:06:32,720 the function execution with this behind the scenes assignment. 87 00:06:32,720 --> 00:06:40,830 It's as if we have s equals Wei and n equals four, 88 00:06:40,830 --> 00:06:44,450 and that those assignment statements are executing behind 89 00:06:44,450 --> 00:06:49,115 the scenes at the beginning of the invocation of hello3. 90 00:06:49,115 --> 00:06:55,200 I'll see you next time for functions that produce outputs by a return values.