1 00:00:08,000 --> 00:00:11,700 Welcome back. Let's take a snippet of code 2 00:00:11,700 --> 00:00:14,760 that we've seen before and turn it into a function, 3 00:00:14,760 --> 00:00:20,440 so we can invoke it at anytime instead of copying and editing the code. 4 00:00:20,750 --> 00:00:24,555 We've seen something like this before, 5 00:00:24,555 --> 00:00:29,300 we're trying to eventually get to defining a function total, 6 00:00:29,300 --> 00:00:33,000 but here's some code that computes the total of a list, 7 00:00:33,000 --> 00:00:35,379 the sum of all the values, 8 00:00:35,510 --> 00:00:38,925 and it uses the accumulator pattern. 9 00:00:38,925 --> 00:00:42,755 Here, we've got a particular list one, five and seven. 10 00:00:42,755 --> 00:00:45,485 We start our accumulator with zero, 11 00:00:45,485 --> 00:00:50,750 we iterate through the list each time updating the total. 12 00:00:50,750 --> 00:00:55,025 The accumulator gets its old value plus the current number, 13 00:00:55,025 --> 00:01:02,250 so the accumulator total starts at zero and it ends up being zero plus one for one, 14 00:01:02,250 --> 00:01:03,930 one plus five makes six, 15 00:01:03,930 --> 00:01:11,080 six plus seven makes 13 and we print out 13. 16 00:01:12,050 --> 00:01:14,230 Now, the question is, 17 00:01:14,230 --> 00:01:19,295 how can we make it work for any list of integers rather than just for one, five, seven? 18 00:01:19,295 --> 00:01:22,415 You can see here that the problem that 19 00:01:22,415 --> 00:01:25,610 we're being asked to do is define a function called total, 20 00:01:25,610 --> 00:01:30,100 and so we're getting an error because total wasn't even defined. 21 00:01:30,100 --> 00:01:33,120 We never created the function called total. 22 00:01:34,430 --> 00:01:39,810 So, let's do that, let's define a function called total. 23 00:01:43,430 --> 00:01:48,185 We'll put all of this code inside that function. 24 00:01:48,185 --> 00:01:51,460 So, we're defining a function total. 25 00:01:51,460 --> 00:01:53,780 It's going to have some parameters, 26 00:01:53,780 --> 00:01:55,775 we're going to come back to that in a second, 27 00:01:55,775 --> 00:02:02,640 and then all of this code we'll put inside here, 28 00:02:02,640 --> 00:02:05,815 and we're going to have to make some adjustments. 29 00:02:05,815 --> 00:02:10,485 The first thing is that we want total to take an input, 30 00:02:10,485 --> 00:02:14,620 we don't want it to just work on this particular list of one, five and seven. 31 00:02:14,620 --> 00:02:16,975 We want it to work on any list. 32 00:02:16,975 --> 00:02:18,880 So, let's make that be 33 00:02:18,880 --> 00:02:23,870 a formal parameter when the total function gets invoked that's when one, 34 00:02:23,870 --> 00:02:27,445 five, seven or some other list will be specified. 35 00:02:27,445 --> 00:02:30,100 So, we're going to have that behind the scenes 36 00:02:30,100 --> 00:02:32,590 assignment to whatever list we want to run this 37 00:02:32,590 --> 00:02:38,085 on rather than having the assignment to a particular value. 38 00:02:38,085 --> 00:02:43,035 Then of course we need to, let's invoke total, 39 00:02:43,035 --> 00:02:52,780 we're going to invoke it and assign the value to a variable called Y. 40 00:02:52,910 --> 00:02:57,290 Let's see what happens in code lens with this. 41 00:02:58,880 --> 00:03:04,505 So, we first get the variable total bound to the function, 42 00:03:04,505 --> 00:03:06,290 we've defined the function, 43 00:03:06,290 --> 00:03:09,940 and now we're invoking it on the list one, five, seven. 44 00:03:09,940 --> 00:03:13,630 So, there's a behind the scenes invocation. 45 00:03:13,630 --> 00:03:21,420 The variable LST now gets bound to the list one, 46 00:03:21,420 --> 00:03:26,240 five, seven and we're ready to execute the lines of code. 47 00:03:26,690 --> 00:03:29,880 We're going tot equal to zero, 48 00:03:29,880 --> 00:03:32,175 and we're going to iterate num is first bound 49 00:03:32,175 --> 00:03:35,025 one then it'll get bound to five, and then to seven. 50 00:03:35,025 --> 00:03:40,995 This time is bound to one and tot gets updated to be zero plus one, 51 00:03:40,995 --> 00:03:42,915 and num is five, 52 00:03:42,915 --> 00:03:48,835 tot gets updated to be one plus five and so on. 53 00:03:48,835 --> 00:03:51,875 Now, we're printing out that total, 54 00:03:51,875 --> 00:03:54,840 and we're returning it. 55 00:03:54,840 --> 00:03:57,660 Whoops! No we're not. 56 00:03:57,660 --> 00:04:02,200 So, this is that gotcha that often gets people when they're new to writing 57 00:04:02,200 --> 00:04:06,745 functions that we don't want to print out the total inside the function, 58 00:04:06,745 --> 00:04:09,665 we want to return the total. 59 00:04:09,665 --> 00:04:13,750 What will happen here is that Y is going to be bound to none, 60 00:04:13,750 --> 00:04:16,870 and we really wanted Y to be bound to 13. 61 00:04:16,870 --> 00:04:21,610 So, the way to fix that is instead of printing the total, 62 00:04:21,610 --> 00:04:24,410 we're going to return the total. 63 00:04:28,220 --> 00:04:33,650 So, before I do that, I just want to show you one other thing that's instructive. 64 00:04:35,480 --> 00:04:39,390 I hide cold lens, if I run this, 65 00:04:39,390 --> 00:04:42,720 I'm going to get errors because our function is not right, 66 00:04:42,720 --> 00:04:45,650 but I'm also going to get a lot of outputs. 67 00:04:46,460 --> 00:04:50,595 The reason is, and this can be confusing, 68 00:04:50,595 --> 00:04:54,860 we have tests, when give you these exercise for example, 69 00:04:54,860 --> 00:04:56,525 writing a function named total, 70 00:04:56,525 --> 00:05:00,530 we add some tests where we behind the scenes are invoking 71 00:05:00,530 --> 00:05:04,520 the function total and checking to make sure that it's giving the right output. 72 00:05:04,520 --> 00:05:08,750 So, for example we're invoking it on the list one, two, three, four, 73 00:05:08,750 --> 00:05:14,055 five and we're expecting the total to be 15. 74 00:05:14,055 --> 00:05:16,770 One plus two plus three plus four plus five. 75 00:05:16,770 --> 00:05:21,230 But the actual value that's getting returned is none, 76 00:05:21,230 --> 00:05:24,290 and so we're getting an error. 77 00:05:24,290 --> 00:05:28,575 But each time we invoke the function, 78 00:05:28,575 --> 00:05:31,135 our line five is running, 79 00:05:31,135 --> 00:05:36,400 and so we're actually printing out the 15. 80 00:05:36,680 --> 00:05:41,120 The 13 is coming from our invocation. 81 00:05:41,120 --> 00:05:44,210 But the 15 is coming from when there's 82 00:05:44,210 --> 00:05:47,300 that behind the scenes invocation for the test then we have 83 00:05:47,300 --> 00:05:49,970 zero when we invoke it on a list of 84 00:05:49,970 --> 00:05:54,385 zeros and we have another zero and when we invoke it on an empty list, 85 00:05:54,385 --> 00:05:57,465 and we have lists of just two. 86 00:05:57,465 --> 00:06:00,475 It's printing out every time, 87 00:06:00,475 --> 00:06:08,015 but it's returning the value none and so our tests are failing. Let's fix that. 88 00:06:08,015 --> 00:06:17,250 Let's return tot, and return does not use parentheses it's statement not a function call. 89 00:06:17,450 --> 00:06:24,120 Now, when we run it, we will pass the tests and we will 90 00:06:24,120 --> 00:06:31,415 not print out anything here in the output window because we had no print statements. 91 00:06:31,415 --> 00:06:35,210 If we wanted to print out Y, 92 00:06:35,210 --> 00:06:38,460 we could get the value 13 to print. 93 00:06:38,750 --> 00:06:45,725 The 13 prints, but we don't get any printouts from the rest of the tests, 94 00:06:45,725 --> 00:06:50,180 all of those cause the code inside total to run, 95 00:06:50,180 --> 00:06:52,525 but it doesn't have any print statements. 96 00:06:52,525 --> 00:06:56,665 So, we don't get any confusing outputs there. 97 00:06:56,665 --> 00:06:59,380 Let's look back at what we did. 98 00:06:59,380 --> 00:07:02,270 We started with our accumulation snippet, 99 00:07:02,270 --> 00:07:05,520 with just one hard-coded list, 100 00:07:05,660 --> 00:07:08,160 and just to remind you. 101 00:07:08,160 --> 00:07:09,710 So, we started with 102 00:07:09,710 --> 00:07:14,695 just this hard-coded snippet that worked to find the total of a particular list. 103 00:07:14,695 --> 00:07:18,780 Then we converted it to a function definition. 104 00:07:19,490 --> 00:07:23,310 The list became a parameter 105 00:07:24,340 --> 00:07:29,950 of the function rather than being hard-coded as a particular one. 106 00:07:29,950 --> 00:07:34,100 The specific list that we were originally working 107 00:07:34,100 --> 00:07:40,150 with became an input or an actual parameter. 108 00:07:40,150 --> 00:07:43,780 It gets bound to the formal parameter. 109 00:07:45,200 --> 00:07:49,145 Then we needed to change the print to return. 110 00:07:49,145 --> 00:07:51,185 We needed to return the total, 111 00:07:51,185 --> 00:07:56,180 and print it out here rather than doing a print inside the function. 112 00:07:56,180 --> 00:07:59,090 This is a common process for abstracting 113 00:07:59,090 --> 00:08:01,820 from a bit of code that works on a particular value, 114 00:08:01,820 --> 00:08:06,425 you make that value be a formal parameter name of the function, 115 00:08:06,425 --> 00:08:09,865 you pass specific values in when you invoke the function. 116 00:08:09,865 --> 00:08:13,010 The challenge is to make the bit of code inside the function 117 00:08:13,010 --> 00:08:16,490 be more general so that it works on any possible input. 118 00:08:16,490 --> 00:08:23,070 In this case, any list of numbers. See you next time.