1 00:00:00,240 --> 00:00:02,740 Hello, and welcome to Chapter Four, 2 00:00:02,740 --> 00:00:05,890 Functions, in the book Python for Informatics. 3 00:00:06,980 --> 00:00:09,560 As always, these slides and this audio and 4 00:00:09,560 --> 00:00:12,990 this video are copyright Creative Commons Attribution. 5 00:00:14,610 --> 00:00:19,220 Now we are to the point, you know, Chapter Four, we're sort of well into the class. 6 00:00:19,220 --> 00:00:23,560 So I figure I should introduce myself a little bit, let you know a little bit. 7 00:00:24,930 --> 00:00:27,270 As I said before, I think in the beginning, 8 00:00:27,270 --> 00:00:30,720 we're tape, I'm taping this in a 9 00:00:30,720 --> 00:00:35,740 wonderful building at the University of Michigan called the North Quad. 10 00:00:35,740 --> 00:00:39,700 It's a relatively new building, it's got some 11 00:00:39,700 --> 00:00:42,470 residential sections and some academic sections and some 12 00:00:42,470 --> 00:00:44,920 classrooms, and one of the classrooms that 13 00:00:44,920 --> 00:00:49,220 I typically teach in is actually 2255 North Quad. 14 00:00:49,220 --> 00:00:50,700 It's a really beautiful room 15 00:00:50,700 --> 00:00:54,190 with great ways for people to interact, and so sometimes 16 00:00:54,190 --> 00:00:58,460 I'm teaching, you know, little tiny Dr. Chuck down here. 17 00:00:58,460 --> 00:00:59,830 With a smile on the face. 18 00:00:59,830 --> 00:01:03,600 And sometimes my students are taking me on, taking my 19 00:01:03,600 --> 00:01:08,720 classes on campus and sometimes students are watching me through lecture. 20 00:01:10,834 --> 00:01:14,110 And so this building is really beautiful and if 21 00:01:14,110 --> 00:01:15,940 you ever get a chance to come to Ann Arbor 22 00:01:15,940 --> 00:01:19,100 and look at it, maybe walk through it, it's really, it's really quite nice. 23 00:01:19,100 --> 00:01:20,910 One of the things I like about it is 24 00:01:20,910 --> 00:01:27,110 that I think it's really highly inspired by Harry Potter. 25 00:01:27,110 --> 00:01:29,940 The kind of, of course, Oxford and Cambridge are 26 00:01:29,940 --> 00:01:34,790 the real inspiration for Harry Potter, but our, our cafeteria, 27 00:01:34,790 --> 00:01:38,650 for example, it kind of looks like the four tables 28 00:01:38,650 --> 00:01:40,990 in Hogwarts and you can kind of imagine a snowy owl 29 00:01:40,990 --> 00:01:46,970 flying around and a Sorting Hat at the, at the front sorting people. 30 00:01:46,970 --> 00:01:53,110 And so the nickname, the nickname for the place is Quadwarts, because it's North Quad 31 00:01:53,110 --> 00:01:59,820 Quadwarts, that's like Hogwarts and North Quad kind of jammed together. 32 00:01:59,820 --> 00:02:01,990 And of course, given that we sort of think 33 00:02:01,990 --> 00:02:06,820 of ourselves a little bit as Harry Potter, 34 00:02:06,820 --> 00:02:11,440 people when they first come in September often sort of decide 35 00:02:11,440 --> 00:02:14,070 to sort themselves and a few years back 36 00:02:14,070 --> 00:02:17,480 when we first started the building the students 37 00:02:17,480 --> 00:02:21,990 decided that I did not get to be in Gryffindor. 38 00:02:21,990 --> 00:02:24,420 As a matter of fact, it's probably time for me to to 39 00:02:24,420 --> 00:02:27,930 show you who I am, and who I've been sorted to be. 40 00:02:27,930 --> 00:02:31,600 So the students decided that I couldn't be in Griffindor. 41 00:02:31,600 --> 00:02:33,980 That I had to be in Slitherin, 42 00:02:33,980 --> 00:02:36,620 and that's because of my name, Charles Severance. 43 00:02:36,620 --> 00:02:38,180 And sever is snake. 44 00:02:38,180 --> 00:02:40,480 What's even cooler, of course, 45 00:02:40,480 --> 00:02:46,110 is given that I teach Python, Slitherin's house is 46 00:02:46,110 --> 00:02:50,300 a snake, right? So it makes a lot of sense, I even have 47 00:02:50,300 --> 00:02:56,150 this really fancy Slitherin teacup that I use to drink tea during lectures. 48 00:02:56,150 --> 00:02:57,160 Sometimes I drink coffee, and 49 00:02:57,160 --> 00:03:00,200 sometimes I drink tea. Oh wow, this thing itches. 50 00:03:00,200 --> 00:03:02,190 So let me just get rid of it. 51 00:03:02,190 --> 00:03:04,060 If I had any hair, that would mess my hair up. 52 00:03:04,060 --> 00:03:06,150 So let me get rid of this for the rest of the lecture. 53 00:03:08,590 --> 00:03:13,295 So there I am, okay. Enough of that, back to, 54 00:03:13,295 --> 00:03:17,195 back to Dr. Chuck. [CLAP] 55 00:03:17,195 --> 00:03:24,940 So, with that sort of brief, brief interlude, the topic of 56 00:03:24,940 --> 00:03:28,780 the actual topic of this lecture is Functions. And so storing and 57 00:03:28,780 --> 00:03:33,830 reusing is basically an idea that 58 00:03:33,830 --> 00:03:37,690 we will often have a series of steps that we will 59 00:03:37,690 --> 00:03:42,110 want to use over and over in a program, increasingly complex. 60 00:03:42,110 --> 00:03:45,490 The things that we'll use in this lecture are kind of silly because 61 00:03:45,490 --> 00:03:48,160 I have to keep them short so the slides don't get too long. 62 00:03:48,160 --> 00:03:53,760 But a good example of, you know, the kind of work is maybe I am going to use 63 00:03:53,760 --> 00:03:59,290 Google's geocoding service and I am going to send some unstructured 64 00:03:59,290 --> 00:04:02,620 data and get a GPS coordinate back. 65 00:04:02,620 --> 00:04:04,800 And that's a service that I want to call and it would 66 00:04:04,800 --> 00:04:07,370 maybe be about this much lines of, this many lines of code. 67 00:04:07,370 --> 00:04:09,250 And I'm going to want to do that all over the place. 68 00:04:09,250 --> 00:04:13,890 So do I want to put this many lines of code 40 places in my program? 69 00:04:13,890 --> 00:04:15,380 Or do I want to put it one place and 70 00:04:15,380 --> 00:04:18,060 then call it in the various places that I need it? 71 00:04:18,060 --> 00:04:20,920 And so that's why I call it the store and the reuse function. 72 00:04:22,220 --> 00:04:25,280 So if we take a look at the simple syntax here. 73 00:04:27,650 --> 00:04:31,790 These things are called functions and in some languages it's called subprograms, 74 00:04:31,790 --> 00:04:36,970 but we call them functions in Python. And the keyword 75 00:04:36,970 --> 00:04:40,320 that we're really going to focus on is def, which stands for 76 00:04:40,320 --> 00:04:46,810 define, and what happens here is when Python sees this def keyword 77 00:04:46,810 --> 00:04:49,030 it actually doesn't run the code. 78 00:04:50,230 --> 00:04:52,690 It says, oh, you're going to make a function and you're 79 00:04:52,690 --> 00:04:57,180 going to kind of turn on a recorder and start recording this code. 80 00:04:57,180 --> 00:04:58,700 So it has a colon at the end of it. 81 00:04:58,700 --> 00:05:01,060 So it has an indented block afterwards. 82 00:05:01,060 --> 00:05:04,050 And so the indented block becomes recorded. 83 00:05:05,230 --> 00:05:07,440 So instead of running the code, like if we just 84 00:05:07,440 --> 00:05:09,870 put print hello and print fun, it would run it. 85 00:05:09,870 --> 00:05:14,550 Instead it says, hey don't run it right now, name it hello. 86 00:05:14,550 --> 00:05:15,900 We give it a name. 87 00:05:15,900 --> 00:05:18,220 It's kind of like a variable. We choose the name. 88 00:05:18,220 --> 00:05:21,290 We've chosen hello as the name of this. 89 00:05:21,290 --> 00:05:23,770 Define it as hello, have it have these two 90 00:05:23,770 --> 00:05:27,130 lines of Python in it, and we'll use it later, okay? 91 00:05:27,130 --> 00:05:29,730 And so that's the function definition. 92 00:05:29,730 --> 00:05:31,210 That's the store phase. 93 00:05:31,210 --> 00:05:34,520 That is it's sort of like, it doesn't really run those lines. 94 00:05:34,520 --> 00:05:37,220 It sort of makes a variable called hello 95 00:05:37,220 --> 00:05:39,730 that actually contains Python code 96 00:05:39,730 --> 00:05:43,200 rather than containing like 12 or a string or something like that 97 00:05:43,200 --> 00:05:45,220 that we've worked with before. 98 00:05:45,220 --> 00:05:50,250 So this is the store part, and then the reuse part is, we then have extended Python. 99 00:05:50,250 --> 00:05:56,610 We now can call our bit of code. So we say, hello, hello name is what we came up with, 100 00:05:56,610 --> 00:06:00,240 parentheses, and then that says, remember that code that I 101 00:06:00,240 --> 00:06:02,700 put in there under the name hello? 102 00:06:02,700 --> 00:06:05,870 Run it now and, so, so, so if I 103 00:06:05,870 --> 00:06:08,650 start looking at that and then it just continues. 104 00:06:08,650 --> 00:06:11,470 So let me kind of clear this and start over again. 105 00:06:11,470 --> 00:06:14,650 And, so if I watch what Python does from the beginning as 106 00:06:14,650 --> 00:06:18,110 it reads here and it goes oh, you're defining a function named hello. 107 00:06:18,110 --> 00:06:22,110 Great, I will sort of remember, remember. 108 00:06:22,110 --> 00:06:24,650 I got that remembered for you, let's continue on. 109 00:06:24,650 --> 00:06:25,720 Oh, hello! 110 00:06:25,720 --> 00:06:27,470 You want me to run that stuff that 111 00:06:27,470 --> 00:06:30,290 you just got done storing under the name hello. 112 00:06:30,290 --> 00:06:34,290 So then it kind of goes and runs it and out comes Hello Fun. 113 00:06:34,290 --> 00:06:38,750 Then after that it runs to the print and out comes print Zip. 114 00:06:38,750 --> 00:06:39,600 And we say, you know what? 115 00:06:39,600 --> 00:06:41,140 I want to reuse that again. 116 00:06:41,140 --> 00:06:44,150 I stored it once, I can reuse it as many times as I want. 117 00:06:44,150 --> 00:06:48,830 And now, hello, and then these two lines of code run a second time. 118 00:06:48,830 --> 00:06:51,930 So we stored them once, gave them a name, and then 119 00:06:51,930 --> 00:06:55,720 ran them twice, in the context of wherever it is we wanted. 120 00:06:55,720 --> 00:07:00,030 Now this is not sort of a profound 121 00:07:00,030 --> 00:07:02,930 a profound reason to use it in this. 122 00:07:02,930 --> 00:07:04,690 I'm just trying to give you the notion that there's a 123 00:07:04,690 --> 00:07:08,970 way to store and name code that then you can retrieve later. 124 00:07:08,970 --> 00:07:10,420 That's really what's going on here. 125 00:07:11,570 --> 00:07:13,470 There's two kind of functions inside of Python, and 126 00:07:13,470 --> 00:07:16,810 we've actually been using them almost from the very first lecture. 127 00:07:16,810 --> 00:07:19,470 And that is there are built-in functions that Python provides 128 00:07:19,470 --> 00:07:24,870 to us like float, raw_input, int, those kinds of functions. 129 00:07:24,870 --> 00:07:28,150 Those are just part of Python but we call them as functions. 130 00:07:28,150 --> 00:07:29,840 The difference is we don't write them. 131 00:07:29,840 --> 00:07:32,860 And then there's user-defined functions, functions that we write. 132 00:07:32,860 --> 00:07:37,490 Functions that create functionality that we want to make use of. 133 00:07:37,490 --> 00:07:42,820 Like encapsulating the ability to compute pay for time and a half for overtime. 134 00:07:42,820 --> 00:07:45,670 And so, we name these things and we 135 00:07:45,670 --> 00:07:48,130 treat them as new reserved words that we've created. 136 00:07:48,130 --> 00:07:50,060 They're kind of an extension to the language 137 00:07:50,060 --> 00:07:52,560 as it were. 138 00:07:53,560 --> 00:07:58,760 So when we're coming along, we define a function with the def keyword, right? 139 00:07:58,760 --> 00:07:59,680 The def keyword 140 00:08:00,760 --> 00:08:04,180 is a reserved word. It's one of the many reserved words back in Chapter One 141 00:08:04,180 --> 00:08:08,520 that we talked about, and it indicates to Python the beginning of a function. 142 00:08:08,520 --> 00:08:12,700 We define it, and then when we call it, which is called invoking, 143 00:08:12,700 --> 00:08:15,600 it's like, we're building it, and then we're invoking it. 144 00:08:15,600 --> 00:08:18,460 And you can build it once, and then invoke it many, many times. 145 00:08:20,250 --> 00:08:24,750 So for example, here is a built-in function called max that 146 00:08:24,750 --> 00:08:27,280 finds the largest character, the sort 147 00:08:27,280 --> 00:08:31,650 of lexicographically largest character, in a string. 148 00:08:31,650 --> 00:08:35,710 And so it's like okay, tell me the maximum character. 149 00:08:35,710 --> 00:08:37,409 And so max is not some code that we've 150 00:08:37,409 --> 00:08:40,789 written but we are invoking a function here and we're 151 00:08:40,789 --> 00:08:43,390 passing in an argument to that. 152 00:08:43,390 --> 00:08:47,820 So the argument is this stuff in between the parentheses. 153 00:08:47,820 --> 00:08:51,360 So the max function can find the maximum of many different things. 154 00:08:51,360 --> 00:08:54,730 At this moment we want it to find the maximum of that particular string. 155 00:08:54,730 --> 00:08:56,370 The highest character 156 00:08:56,370 --> 00:09:00,720 in that particular string. So, this is a left, a right-hand side of 157 00:09:00,720 --> 00:09:05,230 an assignment statement too. So that has to be evaluated to a value. 158 00:09:06,380 --> 00:09:07,740 So it goes into the function, 159 00:09:07,740 --> 00:09:10,230 does whatever things the function wants to do, 160 00:09:10,230 --> 00:09:12,480 and then the function gives us back a value 161 00:09:12,480 --> 00:09:17,110 that becomes the value for max parentheses Hello world. 162 00:09:17,110 --> 00:09:20,390 And that value in this case is the letter w, okay? 163 00:09:20,390 --> 00:09:22,410 Because the letter w was decided to be 164 00:09:22,410 --> 00:09:24,710 the highest letter and that's what max gives us back. 165 00:09:26,120 --> 00:09:30,530 And then when we're done with that, then that w ends up being assigned. 166 00:09:30,530 --> 00:09:31,850 The assignment statement completes. 167 00:09:31,850 --> 00:09:34,480 And so you can think of the function evaluation 168 00:09:34,480 --> 00:09:38,760 as happening as part of the right-hand side expression calculation. 169 00:09:38,760 --> 00:09:40,910 There could a plus here and other stuff 170 00:09:40,910 --> 00:09:43,020 and it's just, at some point, a big expression. 171 00:09:43,020 --> 00:09:46,300 And this one, it's a simple expression with just one function call. 172 00:09:48,530 --> 00:09:50,830 Now if we look at this, there is some code somewhere. 173 00:09:50,830 --> 00:09:52,950 Somebody wrote some code. 174 00:09:52,950 --> 00:09:54,500 It's part of Python. 175 00:09:54,500 --> 00:09:57,980 You didn't write it. There's a max function somewhere. 176 00:09:57,980 --> 00:09:59,980 And you can think of a function as having some input. 177 00:09:59,980 --> 00:10:02,170 It's kind of like a program, that's why 178 00:10:02,170 --> 00:10:04,920 some languages call these things subprograms. 179 00:10:04,920 --> 00:10:08,430 Because they have an input, they do some kind of useful works, whatever 180 00:10:08,430 --> 00:10:12,810 that useful work happens to be, and then they produce some kind of an output. 181 00:10:12,810 --> 00:10:13,720 Right? So Hello world 182 00:10:13,720 --> 00:10:16,740 is the input, a string, the arguments, the thing we're passing in. 183 00:10:16,740 --> 00:10:20,830 Hello world is what's being passed in to the function. 184 00:10:20,830 --> 00:10:25,450 The function is running and then something comes back and is sent back. 185 00:10:25,450 --> 00:10:29,536 So it has input, processing, and output. 186 00:10:29,536 --> 00:10:32,140 Input, processing, and output. So that's how a function. 187 00:10:32,140 --> 00:10:35,060 Some stored code, whether we wrote it or not, 188 00:10:35,060 --> 00:10:37,690 they work the same when we call 189 00:10:37,690 --> 00:10:38,870 functions, right? 190 00:10:38,870 --> 00:10:43,600 So you could think of this as, somewhere inside of the Python library 191 00:10:43,600 --> 00:10:47,470 is some code that maybe has a little def in there, and the name, 192 00:10:47,470 --> 00:10:51,370 they named the function max, and it takes a single parameter. 193 00:10:51,370 --> 00:10:54,060 And it does some blah, blah, blah, blah, 194 00:10:54,060 --> 00:10:58,980 loopy blah, blah stuff, whatever max wants to do. 195 00:10:58,980 --> 00:11:00,680 Whatever we need max to do based on 196 00:11:00,680 --> 00:11:03,460 the specifications that max is supposed to support. 197 00:11:03,460 --> 00:11:04,220 But somewhere 198 00:11:04,220 --> 00:11:06,800 there is code inside of Python 199 00:11:06,800 --> 00:11:09,250 that actually represents the function definition. 200 00:11:09,250 --> 00:11:11,980 It's a built-in function because it comes with Python 201 00:11:11,980 --> 00:11:14,840 and we didn't have to do anything to add it. 202 00:11:14,840 --> 00:11:17,630 So some common built-in functions that we have 203 00:11:17,630 --> 00:11:22,360 been using all along, good examples are the float, which 204 00:11:22,360 --> 00:11:25,500 takes as input anything and 205 00:11:25,500 --> 00:11:29,250 returns you a floating point number version of that. 206 00:11:29,250 --> 00:11:32,120 Type, which takes a parameter of a variable or a constant 207 00:11:32,120 --> 00:11:35,250 and says, what is the type of this. 208 00:11:35,250 --> 00:11:37,730 Float, again converting. 209 00:11:37,730 --> 00:11:39,160 Type, again, and float. 210 00:11:39,160 --> 00:11:42,100 So these are all things that, we've been calling functions all along. 211 00:11:42,100 --> 00:11:45,660 And it passes the input value into the function, the 212 00:11:45,660 --> 00:11:49,090 function runs and then gives us back a return value 213 00:11:49,090 --> 00:11:54,030 which then participates in the rest of the expression on the right-hand side. 214 00:11:54,030 --> 00:11:56,310 You can think of it's pausing the calculation 215 00:11:56,310 --> 00:11:59,880 on the right-hand side, calling the function, 216 00:11:59,880 --> 00:12:01,480 getting the result of the function back, 217 00:12:01,480 --> 00:12:04,070 and then continuing the evaluation of the right-hand side. 218 00:12:04,070 --> 00:12:08,350 Then coming up with whatever value and then printing that value out. 219 00:12:08,350 --> 00:12:13,040 Okay? Another thing that we've done 220 00:12:13,040 --> 00:12:15,040 is we've done string conversions. Right? 221 00:12:15,040 --> 00:12:18,490 So we've converted, in this case, a string to an integer. 222 00:12:18,490 --> 00:12:20,180 And asked what type it is. 223 00:12:20,180 --> 00:12:23,860 We've converted a string to an integer. So, int converts its argument, 224 00:12:23,860 --> 00:12:26,570 whatever that happens to be, into an integer. 225 00:12:26,570 --> 00:12:27,970 So that's just some of the built-in 226 00:12:27,970 --> 00:12:31,430 functions that we have talked about so far. 227 00:12:31,430 --> 00:12:33,590 Now, this becomes more interesting 228 00:12:36,820 --> 00:12:39,180 when we can make our own, own functions. 229 00:12:39,180 --> 00:12:44,330 Oops, there goes my teabag right in the middle of the thing. 230 00:12:45,430 --> 00:12:49,810 Got to take the teabag out, I think it's, whoa, hang on, be right back. 231 00:12:56,240 --> 00:13:02,720 Teabag, okay, there's my tea. So, so we want to make a new function. 232 00:13:02,720 --> 00:13:06,130 Like I said in the other example, we use the def keyword, 233 00:13:08,620 --> 00:13:12,180 the def keyword here, and then we have some indented bit. 234 00:13:12,180 --> 00:13:16,140 We create a name for it and then have some parentheses. 235 00:13:16,140 --> 00:13:19,210 These parentheses will later tell the inputs 236 00:13:19,210 --> 00:13:20,440 that we're going to pass in, but this 237 00:13:20,440 --> 00:13:24,650 function has no input, so we just go parenthesis, parenthesis and then the 238 00:13:24,650 --> 00:13:27,680 all-important colon character which indicates the beginning of an 239 00:13:27,680 --> 00:13:32,360 indented block of Python, that then is, this detects the function. 240 00:13:32,360 --> 00:13:34,940 So, it's important to remember 241 00:13:34,940 --> 00:13:37,690 that while this is executing when Python first 242 00:13:37,690 --> 00:13:40,860 looks at this, it doesn't run these lines of code. 243 00:13:40,860 --> 00:13:44,840 It just remembers them, and names them print lyrics. 244 00:13:44,840 --> 00:13:48,800 So it doesn't cause any printout, it just causes Python to remember. 245 00:13:48,800 --> 00:13:53,940 I probably said that a few too many times. So, so here is 246 00:13:53,940 --> 00:13:58,970 a difficult problem, and I'll let you think about 247 00:13:58,970 --> 00:14:00,160 it for a while. 248 00:14:00,160 --> 00:14:03,110 I want you to kind of mentally go through and execute this code. 249 00:14:05,330 --> 00:14:10,600 And ask what, ask yourself what the output of this program would produce. 250 00:14:11,840 --> 00:14:12,830 How many lines? 251 00:14:12,830 --> 00:14:16,220 How many lines of output would this program produce? 252 00:14:22,190 --> 00:14:26,930 So, how many of you said three? 253 00:14:28,290 --> 00:14:34,040 How many of you said five? Well, the right answer 254 00:14:34,040 --> 00:14:39,780 is actually three. You see five print statements, two, three, 255 00:14:39,780 --> 00:14:46,370 four, five, but two of the print statements are sitting inside of this. 256 00:14:46,370 --> 00:14:51,270 And we never called, we never invoked a function down here, okay? 257 00:14:51,270 --> 00:14:52,420 So, this one, 258 00:14:54,360 --> 00:14:58,990 let's clear this. This one prints, these two get skipped, 259 00:14:58,990 --> 00:15:00,580 this one prints, and this one prints. 260 00:15:00,580 --> 00:15:03,600 So that that's why there are three statements that print. 261 00:15:04,920 --> 00:15:09,960 There is stored, but we never used, a function called print lyrics. 262 00:15:09,960 --> 00:15:13,520 And it's got two statements in it, but we never used it. 263 00:15:13,520 --> 00:15:19,680 So the output of this is Hello Yo 7, and that's because we 264 00:15:19,680 --> 00:15:21,080 never actually invoked it. 265 00:15:21,080 --> 00:15:26,700 We had to say print lyrics parentheses or whatever to cause it to call this. 266 00:15:26,700 --> 00:15:29,260 Okay? That's just to emphasize that as it 267 00:15:29,260 --> 00:15:32,490 looks at it, it does not execute these lines. 268 00:15:33,990 --> 00:15:39,170 So once we've defined a function, once we have given it a name, given it 269 00:15:39,170 --> 00:15:44,550 code that is a part of it, then we can invoke it or call it as many times as we like. 270 00:15:44,550 --> 00:15:45,360 So now, 271 00:15:45,360 --> 00:15:48,800 our little example works a little better if we actually call our function. 272 00:15:51,360 --> 00:15:53,570 Python really doesn't care if you don't call your function. 273 00:15:53,570 --> 00:15:55,850 It's like you told me to make one, I made one. 274 00:15:55,850 --> 00:15:57,890 You didn't use it. There you go. 275 00:15:57,890 --> 00:16:02,860 But if you look at this one now, so here we go, x equals 5. 276 00:16:02,860 --> 00:16:05,530 print Hello, out comes Hello. 277 00:16:05,530 --> 00:16:06,310 Define. 278 00:16:06,310 --> 00:16:08,050 Nothing happens here. Nothing happens here. 279 00:16:08,050 --> 00:16:09,010 It's just remembering. 280 00:16:10,540 --> 00:16:12,870 Okay? Then it says, print Yo. 281 00:16:12,870 --> 00:16:16,434 Then it calls the function print lyrics, which sort of stops us here, 282 00:16:16,434 --> 00:16:20,700 runs these two lines of code. So out comes that and that. 283 00:16:20,700 --> 00:16:26,600 Then it sort of finishes this and it comes back, x equals x plus 2, then it prints x. 284 00:16:26,600 --> 00:16:28,976 That must mean that x is 7, and so out that comes. 285 00:16:28,976 --> 00:16:35,310 And so, again, it's on the first time through. 286 00:16:35,310 --> 00:16:36,560 No, go back, go back, go back. 287 00:16:37,830 --> 00:16:40,500 On the first time through, it doesn't print. 288 00:16:40,500 --> 00:16:41,554 But then when it hits this, 289 00:16:41,554 --> 00:16:42,680 it prints. 290 00:16:42,680 --> 00:16:44,530 You could say print lyrics several more times and 291 00:16:44,530 --> 00:16:46,700 it would run this as many times as it did, 292 00:16:46,700 --> 00:16:50,630 and it needed to, as many times as you want, and it would make output for you. 293 00:16:50,630 --> 00:16:55,150 So you can invoke this is the definition, let's clear this. 294 00:16:56,510 --> 00:17:03,320 This is the definition. This is the call or invoke. 295 00:17:04,020 --> 00:17:06,560 So we are invoking the function, we're calling the function, 296 00:17:06,560 --> 00:17:09,700 we're causing the function to execute. 297 00:17:09,700 --> 00:17:14,109 Here we're just causing the function to be looked at and defined, 298 00:17:14,109 --> 00:17:17,410 but not actually executed. Hope that's clear. 299 00:17:17,410 --> 00:17:23,410 Now, when we pass data into a function, and functions 300 00:17:23,410 --> 00:17:26,319 that don't take data are not as useful as they could be. 301 00:17:26,319 --> 00:17:28,240 There are plenty of things that do, times 302 00:17:28,240 --> 00:17:29,710 that you build a function doesn't take data. 303 00:17:29,710 --> 00:17:31,630 But the most interesting functions are the ones 304 00:17:31,630 --> 00:17:33,760 that you could hand them something to work on and 305 00:17:33,760 --> 00:17:36,780 they could do their work and then come back with whatever. 306 00:17:36,780 --> 00:17:41,790 So this max function is a good example of this, one that's taking an argument. 307 00:17:41,790 --> 00:17:44,085 We call the things in between the parentheses 308 00:17:44,085 --> 00:17:46,380 when we're invoking the function, 309 00:17:46,380 --> 00:17:50,740 we call the things in between the parentheses arguments, okay? 310 00:17:50,740 --> 00:17:52,680 So that's passing into the function. 311 00:17:52,680 --> 00:17:56,580 Feeding data into the function. So we put arguments in between them. 312 00:17:58,900 --> 00:18:03,000 So for example, here we have a little program. 313 00:18:03,000 --> 00:18:04,500 That 314 00:18:05,980 --> 00:18:09,270 that is, it's a function named greet, and now we are 315 00:18:09,270 --> 00:18:12,280 going to define this function and we're going to say, you know what? 316 00:18:12,280 --> 00:18:15,820 I would like to take a parameter, let's take a parameter. 317 00:18:15,820 --> 00:18:17,880 Let's have one parameter come in. 318 00:18:17,880 --> 00:18:19,540 And we need kind of a placeholder for that 319 00:18:19,540 --> 00:18:22,740 parameter, so within the function we're going to use lang. 320 00:18:22,740 --> 00:18:24,640 Now this isn't actually a real variable. 321 00:18:24,640 --> 00:18:28,130 It's kind of like a, it's a placeholder variable. 322 00:18:28,130 --> 00:18:31,070 So this first parameter, whatever it is, 323 00:18:31,070 --> 00:18:32,970 when it's called, is going to be lang. 324 00:18:32,970 --> 00:18:36,560 And so if that first parameter is equal to es, 325 00:18:36,620 --> 00:18:38,730 we're going to print Hola. 326 00:18:38,730 --> 00:18:42,760 And else if it's equal to fr we'll print Bonjour. 327 00:18:42,760 --> 00:18:44,480 And otherwise, we'll print Hello. 328 00:18:44,480 --> 00:18:48,950 So there's apparently three languages in the world, Spanish, French, and English. 329 00:18:48,950 --> 00:18:51,710 And if it's not Spanish or French, then it must be English. 330 00:18:51,710 --> 00:18:56,340 But, I, you have to keep this kind of small, so my screen doesn't get too big. 331 00:18:56,340 --> 00:18:59,160 So this is again just the definition and if you type this 332 00:18:59,160 --> 00:19:02,160 into the interactive thing it gives you this dot dot dot prompt. 333 00:19:02,160 --> 00:19:04,240 And so we now have this thing called greet 334 00:19:04,240 --> 00:19:07,820 and now we've extended Python to add our own function to Python. 335 00:19:07,820 --> 00:19:13,030 And now we can say greet en and so it runs this code 336 00:19:13,030 --> 00:19:18,630 except that en is lang and so that comes, and then it prints Hello. 337 00:19:18,630 --> 00:19:21,350 So out comes Hello. Now later we can say, oh, 338 00:19:21,350 --> 00:19:25,740 I would to do a greeting, but this time I'm going to pass es in as it. 339 00:19:25,740 --> 00:19:32,300 So lang becomes, for this execution, es. And then, so it prints out Hola. 340 00:19:32,300 --> 00:19:39,070 And then the next execution, lang is fr. So it executes this three times but 341 00:19:39,070 --> 00:19:44,480 lang is different each time because we've passed in different parameters each time. 342 00:19:44,480 --> 00:19:47,130 So that's how we can kind of write general-purpose code 343 00:19:47,130 --> 00:19:52,610 inside the function and then reuse that general-purpose code in different ways. 344 00:19:52,610 --> 00:19:55,260 Okay? It's a real powerful, powerful mechanism 345 00:19:55,260 --> 00:19:57,310 that makes functions far more useful. 346 00:20:00,040 --> 00:20:05,170 Now, functions don't necessarily just have to do stuff. 347 00:20:05,170 --> 00:20:08,460 A real powerful mechanism in a function is what we call a return value. 348 00:20:11,270 --> 00:20:13,620 So a function can take its arguments, do some work. 349 00:20:13,620 --> 00:20:14,920 We've seen that. 350 00:20:14,920 --> 00:20:20,550 And then it can return a value. And the key to the return value is, when we call the 351 00:20:20,550 --> 00:20:22,950 function, like we were calling max, 352 00:20:22,950 --> 00:20:26,560 it gives us back some value like the little w, okay? 353 00:20:26,560 --> 00:20:32,540 So here we're going to make a function called greet that takes no parameters. 354 00:20:32,540 --> 00:20:35,210 Doesn't take parameters, but it has another keyword. 355 00:20:35,210 --> 00:20:38,200 It's another reserved word in Python 356 00:20:38,200 --> 00:20:41,070 and whatever we put on this 357 00:20:41,070 --> 00:20:47,250 return statement shows up as the replacement in this expression. 358 00:20:47,250 --> 00:20:50,560 So, whatever greet is, it runs greet and 359 00:20:50,560 --> 00:20:53,290 then the return is kind of a residual value. 360 00:20:53,290 --> 00:20:55,890 So if we say print greet, comma, Glenn. 361 00:20:55,890 --> 00:20:58,190 it says Hello Glenn, because the return value 362 00:20:58,190 --> 00:21:01,045 for the greet function is the string Hello. 363 00:21:01,045 --> 00:21:03,160 And if we say greet Sally, 364 00:21:03,160 --> 00:21:04,970 it says Hello Sally. 365 00:21:04,970 --> 00:21:09,710 And so, and it's run the code twice and the return function, return value has 366 00:21:09,710 --> 00:21:14,100 been put in here instead. And so the Hello came there and the Hello came there, 367 00:21:14,100 --> 00:21:15,510 so we get the two lines. 368 00:21:15,510 --> 00:21:18,660 So, return is a statement that both terminates the 369 00:21:18,660 --> 00:21:22,520 execution of the function and defines the value of 370 00:21:22,520 --> 00:21:25,770 what will be replaced when the function call comes back, 371 00:21:25,770 --> 00:21:28,150 in the line that the function was called from. 372 00:21:29,980 --> 00:21:35,180 So here is a little smarter version of our greet function. 373 00:21:35,180 --> 00:21:40,340 It's very similar, it's called greet still, takes lang as a parameter. 374 00:21:40,340 --> 00:21:44,470 And if the language is es, then it returns the string Hola. 375 00:21:44,470 --> 00:21:46,760 If the language is French, it returns Bonjour. 376 00:21:46,760 --> 00:21:47,950 Otherwise, it returns Hello. 377 00:21:47,950 --> 00:21:49,570 So we're not actually doing the print, if you 378 00:21:49,570 --> 00:21:51,500 go back on the other slides, we were printing. 379 00:21:51,500 --> 00:21:55,620 But now we're just returning a string. Okay? 380 00:21:55,620 --> 00:22:00,880 And so now, I can call print greet, and pass en in, 381 00:22:00,880 --> 00:22:04,090 so then that runs the code once, with lang equal to en. 382 00:22:04,090 --> 00:22:06,880 And I get back Hello, and then comma, Glenn. 383 00:22:06,880 --> 00:22:09,470 Then I call it again and I pass es in. And then 384 00:22:09,470 --> 00:22:14,920 that time it returns, the return value here becomes Hola, a string Hola. 385 00:22:14,920 --> 00:22:19,870 Hola Sally. And then Michael, I'll pass in one more time. 386 00:22:19,870 --> 00:22:20,820 Lang is now fr, 387 00:22:20,820 --> 00:22:25,990 the string fr, and so it returns a Bonjour and 388 00:22:25,990 --> 00:22:31,620 so the, the residual that is here is Bonjour, and so out comes Bonjour Michael. 389 00:22:31,620 --> 00:22:33,800 So there is lot to this, right? 390 00:22:33,800 --> 00:22:34,910 You're passing stuff in. 391 00:22:34,910 --> 00:22:37,520 You have this kind of placeholder variable. 392 00:22:37,520 --> 00:22:42,020 And you have this return that sort of appears where it was called from. 393 00:22:42,020 --> 00:22:45,670 This goes in, does its work, it comes back, and there's sort of the 394 00:22:45,670 --> 00:22:47,410 residual value that sits here. 395 00:22:47,410 --> 00:22:50,940 You don't have to have a return in a function, but if you want to 396 00:22:50,940 --> 00:22:55,140 do something with the value, then you have to have a return in the function. 397 00:22:55,140 --> 00:22:58,320 We call the functions that produce values 398 00:22:58,320 --> 00:23:01,575 fruitful, and the other ones are called void. 399 00:23:01,575 --> 00:23:04,500 [LAUGH] So, that's a good name for them. 400 00:23:05,630 --> 00:23:07,800 So, to review sort of this, arguments, 401 00:23:07,800 --> 00:23:10,630 parameters, and results, if we look at max, 402 00:23:10,630 --> 00:23:15,960 the original thing, where it's looking for the largest, lexicographically 403 00:23:15,960 --> 00:23:21,950 largest letter, it looks Hello world is the argument that's passed in. 404 00:23:21,950 --> 00:23:25,420 We have this sort of formal parameter here called inp which is not really a 405 00:23:25,420 --> 00:23:28,430 variable, it just happens to refer to whatever is the 406 00:23:28,430 --> 00:23:32,250 first argument in any particular call. 407 00:23:32,250 --> 00:23:35,950 And then it does its little thing and runs loops and does all these things and 408 00:23:35,950 --> 00:23:41,180 at some point it returns w, so that the thing that comes out when the function 409 00:23:41,180 --> 00:23:47,020 quits that becomes the replacement value here is a lowercase w string. 410 00:23:47,020 --> 00:23:49,590 And then that is the w that goes over in the big. 411 00:23:49,590 --> 00:23:54,890 So the return is what defines what comes back here. 412 00:23:54,890 --> 00:23:58,850 Because you think of this as, it's looking at this. It suspends for the moment. 413 00:23:58,850 --> 00:24:00,300 It runs this code. 414 00:24:00,300 --> 00:24:00,890 It's holding. 415 00:24:00,890 --> 00:24:02,310 It's holding itself here. 416 00:24:02,310 --> 00:24:05,080 It's running this code and then it comes back to here. 417 00:24:06,220 --> 00:24:07,190 Okay? 418 00:24:07,190 --> 00:24:10,200 And the return value is what defines coming back. 419 00:24:11,260 --> 00:24:17,510 So, of course you can have more than one parameter and they are in order. 420 00:24:17,510 --> 00:24:18,820 So here we have an a and a b. 421 00:24:19,860 --> 00:24:22,390 These, the name of these things doesn't really matter. 422 00:24:22,390 --> 00:24:25,890 They're just relevant inside of the function definition. 423 00:24:25,890 --> 00:24:28,170 So we are going to add two numbers together 424 00:24:28,170 --> 00:24:31,860 by taking a + b, and then returning the sum. 425 00:24:31,860 --> 00:24:34,130 The added variable is just kind of local to this function. 426 00:24:34,130 --> 00:24:38,140 And now we can say, you know, addtwo, 3, comma, 5. 427 00:24:38,140 --> 00:24:40,310 And then this will come back as 8, and then 8 will get assigned 428 00:24:40,310 --> 00:24:42,990 into x, and so that will print out 8. 429 00:24:42,990 --> 00:24:45,340 And so you can have as many of these as you want 430 00:24:45,340 --> 00:24:49,275 and the order matters and there is a one-to-one correspondence. 431 00:24:49,275 --> 00:24:51,040 3 goes to a and 5 goes to b 432 00:24:51,040 --> 00:24:52,380 when the thing is called. 433 00:24:52,380 --> 00:24:56,410 And then the return value again comes back. 434 00:24:56,410 --> 00:25:01,080 Okay? So that's, sort of, arguments. 435 00:25:01,080 --> 00:25:04,850 And like I said, not all functions have to return values. 436 00:25:04,850 --> 00:25:07,980 We call them void functions when they don't return anything. 437 00:25:07,980 --> 00:25:10,860 It's totally fine for that to be the case. 438 00:25:12,490 --> 00:25:16,350 So at this point you might be thinking to yourself, okay, great, well I still 439 00:25:16,350 --> 00:25:18,320 don't quite get why to use functions. 440 00:25:18,320 --> 00:25:22,890 And in reality, in the first 10, 11 chapters of this book, other than 441 00:25:22,890 --> 00:25:26,470 using lots of functions, we're not really going to spend a lot 442 00:25:26,470 --> 00:25:30,500 of time making functions because most of our programs are going to kind of be 443 00:25:30,500 --> 00:25:33,650 that long and we're not going to do a lot reuse in the program. 444 00:25:33,650 --> 00:25:39,040 And there'll be a time when your programs become complex enough. 445 00:25:39,040 --> 00:25:41,450 You'll be like, oh, thank heaven for functions. 446 00:25:41,450 --> 00:25:44,580 I think it's premature to say you must use functions, even though 447 00:25:44,580 --> 00:25:47,860 there are some exercises that just say, hey, do this with a function. 448 00:25:47,860 --> 00:25:49,800 Just so you kind of get the understanding of a function. 449 00:25:50,880 --> 00:25:52,369 You will find soon enough, 450 00:25:53,550 --> 00:25:55,790 as your programs grow, you'll go like, oh, 451 00:25:55,790 --> 00:25:57,480 I keep doing this same thing over and over again. 452 00:25:57,480 --> 00:25:59,280 Let me pull it up into a function and pass 453 00:25:59,280 --> 00:26:02,630 a parameter in, have a return value, and away you go. 454 00:26:02,630 --> 00:26:04,560 Or, you might find that you're moving from 455 00:26:04,560 --> 00:26:06,480 one program to another and you have this common thing 456 00:26:06,480 --> 00:26:09,570 that you want to do, so you make yourself a library that you drag along. 457 00:26:09,570 --> 00:26:12,190 And we will do lots of libraries. 458 00:26:12,190 --> 00:26:14,910 The book in the second half does lots and lots of library 459 00:26:14,910 --> 00:26:18,880 stuff, doing things like parsing XML and this, that, and the other thing. 460 00:26:18,880 --> 00:26:24,130 So, so don't feel like you need to use functions on every 461 00:26:24,130 --> 00:26:27,930 assignment, because they're a natural thing when a program gets big enough. 462 00:26:27,930 --> 00:26:30,920 So, so just kind of understand them on a 463 00:26:30,920 --> 00:26:32,960 mechanical level, but 464 00:26:32,960 --> 00:26:34,930 it'll come to you at the right time 465 00:26:34,930 --> 00:26:38,000 when it's time to start building your own functions. 466 00:26:38,000 --> 00:26:40,850 So in this class, we kind of, you know, talked about functions. 467 00:26:40,850 --> 00:26:41,730 Just got you started. 468 00:26:41,730 --> 00:26:45,180 Talked about parameters, talked about built-in functions, 469 00:26:45,180 --> 00:26:50,290 talked about return values, the store and reuse pattern. 470 00:26:50,290 --> 00:26:52,630 So, the problems at the end of the 471 00:26:52,630 --> 00:26:57,100 chapter for this particular chapter are relatively straightforward, 472 00:26:57,100 --> 00:27:00,790 in that, like I said, we don't have a real strong 473 00:27:00,790 --> 00:27:05,630 need to do functions yet in this class because the programs aren't large enough. 474 00:27:05,630 --> 00:27:08,017 But I just said okay, take, take one 475 00:27:08,017 --> 00:27:12,060 of your previous assignments and refactor the code 476 00:27:12,060 --> 00:27:16,880 so that at the top there is a def computepay, and you put like the if 477 00:27:16,880 --> 00:27:19,830 and whatever in here, and then later on 478 00:27:19,830 --> 00:27:22,270 you do your code and then you call computepay. 479 00:27:22,270 --> 00:27:24,200 So you took code that you already had, you 480 00:27:24,200 --> 00:27:27,140 move it up into a function, and make a function. 481 00:27:27,140 --> 00:27:29,160 And I've also online got sort of a 482 00:27:29,160 --> 00:27:31,810 sample of this, because it's, it's a little complex. 483 00:27:31,810 --> 00:27:36,820 And so you should be able to find, on Python Learn or on the course site, 484 00:27:38,140 --> 00:27:39,760 you should be able to find a good example, 485 00:27:39,760 --> 00:27:41,670 because I really want you to sort of get this. 486 00:27:43,320 --> 00:27:45,290 Like I said, there will come a time 487 00:27:45,290 --> 00:27:47,740 when functions will make the most sense to you. 488 00:27:47,740 --> 00:27:51,990 But coming up next, of course, is Chapter Five, and that's loops. 489 00:27:51,990 --> 00:27:55,110 And loops are going to rock the house, And so we really, that's our 490 00:27:55,110 --> 00:28:00,530 fourth major pattern is loops and and I'm looking forward to it. 491 00:28:00,530 --> 00:28:02,620 So we'll we'll see you at the next lecture.