WEBVTT 00:00:00.240 --> 00:00:02.740 Hello, and welcome to Chapter Four, 00:00:02.740 --> 00:00:05.890 Functions, in the book Python for Informatics. 00:00:06.980 --> 00:00:09.560 As always, these slides and this audio and 00:00:09.560 --> 00:00:12.990 this video are copyright Creative Commons Attribution. 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. 00:00:19.220 --> 00:00:23.560 So I figure I should introduce myself a little bit, let you know a little bit. 00:00:24.930 --> 00:00:27.270 As I said before, I think in the beginning, 00:00:27.270 --> 00:00:30.720 we're tape, I'm taping this in a 00:00:30.720 --> 00:00:35.740 wonderful building at the University of Michigan called the North Quad. 00:00:35.740 --> 00:00:39.700 It's a relatively new building, it's got some 00:00:39.700 --> 00:00:42.470 residential sections and some academic sections and some 00:00:42.470 --> 00:00:44.920 classrooms, and one of the classrooms that 00:00:44.920 --> 00:00:49.220 I typically teach in is actually 2255 North Quad. 00:00:49.220 --> 00:00:50.700 It's a really beautiful room 00:00:50.700 --> 00:00:54.190 with great ways for people to interact, and so sometimes 00:00:54.190 --> 00:00:58.460 I'm teaching, you know, little tiny Dr. Chuck down here. 00:00:58.460 --> 00:00:59.830 With a smile on the face. 00:00:59.830 --> 00:01:03.600 And sometimes my students are taking me on, taking my 00:01:03.600 --> 00:01:08.720 classes on campus and sometimes students are watching me through lecture. 00:01:10.834 --> 00:01:14.110 And so this building is really beautiful and if 00:01:14.110 --> 00:01:15.940 you ever get a chance to come to Ann Arbor 00:01:15.940 --> 00:01:19.100 and look at it, maybe walk through it, it's really, it's really quite nice. 00:01:19.100 --> 00:01:20.910 One of the things I like about it is 00:01:20.910 --> 00:01:27.110 that I think it's really highly inspired by Harry Potter. 00:01:27.110 --> 00:01:29.940 The kind of, of course, Oxford and Cambridge are 00:01:29.940 --> 00:01:34.790 the real inspiration for Harry Potter, but our, our cafeteria, 00:01:34.790 --> 00:01:38.650 for example, it kind of looks like the four tables 00:01:38.650 --> 00:01:40.990 in Hogwarts and you can kind of imagine a snowy owl 00:01:40.990 --> 00:01:46.970 flying around and a Sorting Hat at the, at the front sorting people. 00:01:46.970 --> 00:01:53.110 And so the nickname, the nickname for the place is Quadwarts, because it's North Quad 00:01:53.110 --> 00:01:59.820 Quadwarts, that's like Hogwarts and North Quad kind of jammed together. 00:01:59.820 --> 00:02:01.990 And of course, given that we sort of think 00:02:01.990 --> 00:02:06.820 of ourselves a little bit as Harry Potter, 00:02:06.820 --> 00:02:11.440 people when they first come in September often sort of decide 00:02:11.440 --> 00:02:14.070 to sort themselves and a few years back 00:02:14.070 --> 00:02:17.480 when we first started the building the students 00:02:17.480 --> 00:02:21.990 decided that I did not get to be in Gryffindor. 00:02:21.990 --> 00:02:24.420 As a matter of fact, it's probably time for me to to 00:02:24.420 --> 00:02:27.930 show you who I am, and who I've been sorted to be. 00:02:27.930 --> 00:02:31.600 So the students decided that I couldn't be in Griffindor. 00:02:31.600 --> 00:02:33.980 That I had to be in Slitherin, 00:02:33.980 --> 00:02:36.620 and that's because of my name, Charles Severance. 00:02:36.620 --> 00:02:38.180 And sever is snake. 00:02:38.180 --> 00:02:40.480 What's even cooler, of course, 00:02:40.480 --> 00:02:46.110 is given that I teach Python, Slitherin's house is 00:02:46.110 --> 00:02:50.300 a snake, right? So it makes a lot of sense, I even have 00:02:50.300 --> 00:02:56.150 this really fancy Slitherin teacup that I use to drink tea during lectures. 00:02:56.150 --> 00:02:57.160 Sometimes I drink coffee, and 00:02:57.160 --> 00:03:00.200 sometimes I drink tea. Oh wow, this thing itches. 00:03:00.200 --> 00:03:02.190 So let me just get rid of it. 00:03:02.190 --> 00:03:04.060 If I had any hair, that would mess my hair up. 00:03:04.060 --> 00:03:06.150 So let me get rid of this for the rest of the lecture. 00:03:08.590 --> 00:03:13.295 So there I am, okay. Enough of that, back to, 00:03:13.295 --> 00:03:17.195 back to Dr. Chuck. [CLAP] 00:03:17.195 --> 00:03:24.940 So, with that sort of brief, brief interlude, the topic of 00:03:24.940 --> 00:03:28.780 the actual topic of this lecture is Functions. And so storing and 00:03:28.780 --> 00:03:33.830 reusing is basically an idea that 00:03:33.830 --> 00:03:37.690 we will often have a series of steps that we will 00:03:37.690 --> 00:03:42.110 want to use over and over in a program, increasingly complex. 00:03:42.110 --> 00:03:45.490 The things that we'll use in this lecture are kind of silly because 00:03:45.490 --> 00:03:48.160 I have to keep them short so the slides don't get too long. 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 00:03:53.760 --> 00:03:59.290 Google's geocoding service and I am going to send some unstructured 00:03:59.290 --> 00:04:02.620 data and get a GPS coordinate back. 00:04:02.620 --> 00:04:04.800 And that's a service that I want to call and it would 00:04:04.800 --> 00:04:07.370 maybe be about this much lines of, this many lines of code. 00:04:07.370 --> 00:04:09.250 And I'm going to want to do that all over the place. 00:04:09.250 --> 00:04:13.890 So do I want to put this many lines of code 40 places in my program? 00:04:13.890 --> 00:04:15.380 Or do I want to put it one place and 00:04:15.380 --> 00:04:18.060 then call it in the various places that I need it? 00:04:18.060 --> 00:04:20.920 And so that's why I call it the store and the reuse function. 00:04:22.220 --> 00:04:25.280 So if we take a look at the simple syntax here. 00:04:27.650 --> 00:04:31.790 These things are called functions and in some languages it's called subprograms, 00:04:31.790 --> 00:04:36.970 but we call them functions in Python. And the keyword 00:04:36.970 --> 00:04:40.320 that we're really going to focus on is def, which stands for 00:04:40.320 --> 00:04:46.810 define, and what happens here is when Python sees this def keyword 00:04:46.810 --> 00:04:49.030 it actually doesn't run the code. 00:04:50.230 --> 00:04:52.690 It says, oh, you're going to make a function and you're 00:04:52.690 --> 00:04:57.180 going to kind of turn on a recorder and start recording this code. 00:04:57.180 --> 00:04:58.700 So it has a colon at the end of it. 00:04:58.700 --> 00:05:01.060 So it has an indented block afterwards. 00:05:01.060 --> 00:05:04.050 And so the indented block becomes recorded. 00:05:05.230 --> 00:05:07.440 So instead of running the code, like if we just 00:05:07.440 --> 00:05:09.870 put print hello and print fun, it would run it. 00:05:09.870 --> 00:05:14.550 Instead it says, hey don't run it right now, name it hello. 00:05:14.550 --> 00:05:15.900 We give it a name. 00:05:15.900 --> 00:05:18.220 It's kind of like a variable. We choose the name. 00:05:18.220 --> 00:05:21.290 We've chosen hello as the name of this. 00:05:21.290 --> 00:05:23.770 Define it as hello, have it have these two 00:05:23.770 --> 00:05:27.130 lines of Python in it, and we'll use it later, okay? 00:05:27.130 --> 00:05:29.730 And so that's the function definition. 00:05:29.730 --> 00:05:31.210 That's the store phase. 00:05:31.210 --> 00:05:34.520 That is it's sort of like, it doesn't really run those lines. 00:05:34.520 --> 00:05:37.220 It sort of makes a variable called hello 00:05:37.220 --> 00:05:39.730 that actually contains Python code 00:05:39.730 --> 00:05:43.200 rather than containing like 12 or a string or something like that 00:05:43.200 --> 00:05:45.220 that we've worked with before. 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. 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, 00:05:56.610 --> 00:06:00.240 parentheses, and then that says, remember that code that I 00:06:00.240 --> 00:06:02.700 put in there under the name hello? 00:06:02.700 --> 00:06:05.870 Run it now and, so, so, so if I 00:06:05.870 --> 00:06:08.650 start looking at that and then it just continues. 00:06:08.650 --> 00:06:11.470 So let me kind of clear this and start over again. 00:06:11.470 --> 00:06:14.650 And, so if I watch what Python does from the beginning as 00:06:14.650 --> 00:06:18.110 it reads here and it goes oh, you're defining a function named hello. 00:06:18.110 --> 00:06:22.110 Great, I will sort of remember, remember. 00:06:22.110 --> 00:06:24.650 I got that remembered for you, let's continue on. 00:06:24.650 --> 00:06:25.720 Oh, hello! 00:06:25.720 --> 00:06:27.470 You want me to run that stuff that 00:06:27.470 --> 00:06:30.290 you just got done storing under the name hello. 00:06:30.290 --> 00:06:34.290 So then it kind of goes and runs it and out comes Hello Fun. 00:06:34.290 --> 00:06:38.750 Then after that it runs to the print and out comes print Zip. 00:06:38.750 --> 00:06:39.600 And we say, you know what? 00:06:39.600 --> 00:06:41.140 I want to reuse that again. 00:06:41.140 --> 00:06:44.150 I stored it once, I can reuse it as many times as I want. 00:06:44.150 --> 00:06:48.830 And now, hello, and then these two lines of code run a second time. 00:06:48.830 --> 00:06:51.930 So we stored them once, gave them a name, and then 00:06:51.930 --> 00:06:55.720 ran them twice, in the context of wherever it is we wanted. 00:06:55.720 --> 00:07:00.030 Now this is not sort of a profound 00:07:00.030 --> 00:07:02.930 a profound reason to use it in this. 00:07:02.930 --> 00:07:04.690 I'm just trying to give you the notion that there's a 00:07:04.690 --> 00:07:08.970 way to store and name code that then you can retrieve later. 00:07:08.970 --> 00:07:10.420 That's really what's going on here. 00:07:11.570 --> 00:07:13.470 There's two kind of functions inside of Python, and 00:07:13.470 --> 00:07:16.810 we've actually been using them almost from the very first lecture. 00:07:16.810 --> 00:07:19.470 And that is there are built-in functions that Python provides 00:07:19.470 --> 00:07:24.870 to us like float, raw_input, int, those kinds of functions. 00:07:24.870 --> 00:07:28.150 Those are just part of Python but we call them as functions. 00:07:28.150 --> 00:07:29.840 The difference is we don't write them. 00:07:29.840 --> 00:07:32.860 And then there's user-defined functions, functions that we write. 00:07:32.860 --> 00:07:37.490 Functions that create functionality that we want to make use of. 00:07:37.490 --> 00:07:42.820 Like encapsulating the ability to compute pay for time and a half for overtime. 00:07:42.820 --> 00:07:45.670 And so, we name these things and we 00:07:45.670 --> 00:07:48.130 treat them as new reserved words that we've created. 00:07:48.130 --> 00:07:50.060 They're kind of an extension to the language 00:07:50.060 --> 00:07:52.560 as it were. 00:07:53.560 --> 00:07:58.760 So when we're coming along, we define a function with the def keyword, right? 00:07:58.760 --> 00:07:59.680 The def keyword 00:08:00.760 --> 00:08:04.180 is a reserved word. It's one of the many reserved words back in Chapter One 00:08:04.180 --> 00:08:08.520 that we talked about, and it indicates to Python the beginning of a function. 00:08:08.520 --> 00:08:12.700 We define it, and then when we call it, which is called invoking, 00:08:12.700 --> 00:08:15.600 it's like, we're building it, and then we're invoking it. 00:08:15.600 --> 00:08:18.460 And you can build it once, and then invoke it many, many times. 00:08:20.250 --> 00:08:24.750 So for example, here is a built-in function called max that 00:08:24.750 --> 00:08:27.280 finds the largest character, the sort 00:08:27.280 --> 00:08:31.650 of lexicographically largest character, in a string. 00:08:31.650 --> 00:08:35.710 And so it's like okay, tell me the maximum character. 00:08:35.710 --> 00:08:37.409 And so max is not some code that we've 00:08:37.409 --> 00:08:40.789 written but we are invoking a function here and we're 00:08:40.789 --> 00:08:43.390 passing in an argument to that. 00:08:43.390 --> 00:08:47.820 So the argument is this stuff in between the parentheses. 00:08:47.820 --> 00:08:51.360 So the max function can find the maximum of many different things. 00:08:51.360 --> 00:08:54.730 At this moment we want it to find the maximum of that particular string. 00:08:54.730 --> 00:08:56.370 The highest character 00:08:56.370 --> 00:09:00.720 in that particular string. So, this is a left, a right-hand side of 00:09:00.720 --> 00:09:05.230 an assignment statement too. So that has to be evaluated to a value. 00:09:06.380 --> 00:09:07.740 So it goes into the function, 00:09:07.740 --> 00:09:10.230 does whatever things the function wants to do, 00:09:10.230 --> 00:09:12.480 and then the function gives us back a value 00:09:12.480 --> 00:09:17.110 that becomes the value for max parentheses Hello world. 00:09:17.110 --> 00:09:20.390 And that value in this case is the letter w, okay? 00:09:20.390 --> 00:09:22.410 Because the letter w was decided to be 00:09:22.410 --> 00:09:24.710 the highest letter and that's what max gives us back. 00:09:26.120 --> 00:09:30.530 And then when we're done with that, then that w ends up being assigned. 00:09:30.530 --> 00:09:31.850 The assignment statement completes. 00:09:31.850 --> 00:09:34.480 And so you can think of the function evaluation 00:09:34.480 --> 00:09:38.760 as happening as part of the right-hand side expression calculation. 00:09:38.760 --> 00:09:40.910 There could a plus here and other stuff 00:09:40.910 --> 00:09:43.020 and it's just, at some point, a big expression. 00:09:43.020 --> 00:09:46.300 And this one, it's a simple expression with just one function call. 00:09:48.530 --> 00:09:50.830 Now if we look at this, there is some code somewhere. 00:09:50.830 --> 00:09:52.950 Somebody wrote some code. 00:09:52.950 --> 00:09:54.500 It's part of Python. 00:09:54.500 --> 00:09:57.980 You didn't write it. There's a max function somewhere. 00:09:57.980 --> 00:09:59.980 And you can think of a function as having some input. 00:09:59.980 --> 00:10:02.170 It's kind of like a program, that's why 00:10:02.170 --> 00:10:04.920 some languages call these things subprograms. 00:10:04.920 --> 00:10:08.430 Because they have an input, they do some kind of useful works, whatever 00:10:08.430 --> 00:10:12.810 that useful work happens to be, and then they produce some kind of an output. 00:10:12.810 --> 00:10:13.720 Right? So Hello world 00:10:13.720 --> 00:10:16.740 is the input, a string, the arguments, the thing we're passing in. 00:10:16.740 --> 00:10:20.830 Hello world is what's being passed in to the function. 00:10:20.830 --> 00:10:25.450 The function is running and then something comes back and is sent back. 00:10:25.450 --> 00:10:29.536 So it has input, processing, and output. 00:10:29.536 --> 00:10:32.140 Input, processing, and output. So that's how a function. 00:10:32.140 --> 00:10:35.060 Some stored code, whether we wrote it or not, 00:10:35.060 --> 00:10:37.690 they work the same when we call 00:10:37.690 --> 00:10:38.870 functions, right? 00:10:38.870 --> 00:10:43.600 So you could think of this as, somewhere inside of the Python library 00:10:43.600 --> 00:10:47.470 is some code that maybe has a little def in there, and the name, 00:10:47.470 --> 00:10:51.370 they named the function max, and it takes a single parameter. 00:10:51.370 --> 00:10:54.060 And it does some blah, blah, blah, blah, 00:10:54.060 --> 00:10:58.980 loopy blah, blah stuff, whatever max wants to do. 00:10:58.980 --> 00:11:00.680 Whatever we need max to do based on 00:11:00.680 --> 00:11:03.460 the specifications that max is supposed to support. 00:11:03.460 --> 00:11:04.220 But somewhere 00:11:04.220 --> 00:11:06.800 there is code inside of Python 00:11:06.800 --> 00:11:09.250 that actually represents the function definition. 00:11:09.250 --> 00:11:11.980 It's a built-in function because it comes with Python 00:11:11.980 --> 00:11:14.840 and we didn't have to do anything to add it. 00:11:14.840 --> 00:11:17.630 So some common built-in functions that we have 00:11:17.630 --> 00:11:22.360 been using all along, good examples are the float, which 00:11:22.360 --> 00:11:25.500 takes as input anything and 00:11:25.500 --> 00:11:29.250 returns you a floating point number version of that. 00:11:29.250 --> 00:11:32.120 Type, which takes a parameter of a variable or a constant 00:11:32.120 --> 00:11:35.250 and says, what is the type of this. 00:11:35.250 --> 00:11:37.730 Float, again converting. 00:11:37.730 --> 00:11:39.160 Type, again, and float. 00:11:39.160 --> 00:11:42.100 So these are all things that, we've been calling functions all along. 00:11:42.100 --> 00:11:45.660 And it passes the input value into the function, the 00:11:45.660 --> 00:11:49.090 function runs and then gives us back a return value 00:11:49.090 --> 00:11:54.030 which then participates in the rest of the expression on the right-hand side. 00:11:54.030 --> 00:11:56.310 You can think of it's pausing the calculation 00:11:56.310 --> 00:11:59.880 on the right-hand side, calling the function, 00:11:59.880 --> 00:12:01.480 getting the result of the function back, 00:12:01.480 --> 00:12:04.070 and then continuing the evaluation of the right-hand side. 00:12:04.070 --> 00:12:08.350 Then coming up with whatever value and then printing that value out. 00:12:08.350 --> 00:12:13.040 Okay? Another thing that we've done 00:12:13.040 --> 00:12:15.040 is we've done string conversions. Right? 00:12:15.040 --> 00:12:18.490 So we've converted, in this case, a string to an integer. 00:12:18.490 --> 00:12:20.180 And asked what type it is. 00:12:20.180 --> 00:12:23.860 We've converted a string to an integer. So, int converts its argument, 00:12:23.860 --> 00:12:26.570 whatever that happens to be, into an integer. 00:12:26.570 --> 00:12:27.970 So that's just some of the built-in 00:12:27.970 --> 00:12:31.430 functions that we have talked about so far. 00:12:31.430 --> 00:12:33.590 Now, this becomes more interesting 00:12:36.820 --> 00:12:39.180 when we can make our own, own functions. 00:12:39.180 --> 00:12:44.330 Oops, there goes my teabag right in the middle of the thing. 00:12:45.430 --> 00:12:49.810 Got to take the teabag out, I think it's, whoa, hang on, be right back. 00:12:56.240 --> 00:13:02.720 Teabag, okay, there's my tea. So, so we want to make a new function. 00:13:02.720 --> 00:13:06.130 Like I said in the other example, we use the def keyword, 00:13:08.620 --> 00:13:12.180 the def keyword here, and then we have some indented bit. 00:13:12.180 --> 00:13:16.140 We create a name for it and then have some parentheses. 00:13:16.140 --> 00:13:19.210 These parentheses will later tell the inputs 00:13:19.210 --> 00:13:20.440 that we're going to pass in, but this 00:13:20.440 --> 00:13:24.650 function has no input, so we just go parenthesis, parenthesis and then the 00:13:24.650 --> 00:13:27.680 all-important colon character which indicates the beginning of an 00:13:27.680 --> 00:13:32.360 indented block of Python, that then is, this detects the function. 00:13:32.360 --> 00:13:34.940 So, it's important to remember 00:13:34.940 --> 00:13:37.690 that while this is executing when Python first 00:13:37.690 --> 00:13:40.860 looks at this, it doesn't run these lines of code. 00:13:40.860 --> 00:13:44.840 It just remembers them, and names them print lyrics. 00:13:44.840 --> 00:13:48.800 So it doesn't cause any printout, it just causes Python to remember. 00:13:48.800 --> 00:13:53.940 I probably said that a few too many times. So, so here is 00:13:53.940 --> 00:13:58.970 a difficult problem, and I'll let you think about 00:13:58.970 --> 00:14:00.160 it for a while. 00:14:00.160 --> 00:14:03.110 I want you to kind of mentally go through and execute this code. 00:14:05.330 --> 00:14:10.600 And ask what, ask yourself what the output of this program would produce. 00:14:11.840 --> 00:14:12.830 How many lines? 00:14:12.830 --> 00:14:16.220 How many lines of output would this program produce? 00:14:22.190 --> 00:14:26.930 So, how many of you said three? 00:14:28.290 --> 00:14:34.040 How many of you said five? Well, the right answer 00:14:34.040 --> 00:14:39.780 is actually three. You see five print statements, two, three, 00:14:39.780 --> 00:14:46.370 four, five, but two of the print statements are sitting inside of this. 00:14:46.370 --> 00:14:51.270 And we never called, we never invoked a function down here, okay? 00:14:51.270 --> 00:14:52.420 So, this one, 00:14:54.360 --> 00:14:58.990 let's clear this. This one prints, these two get skipped, 00:14:58.990 --> 00:15:00.580 this one prints, and this one prints. 00:15:00.580 --> 00:15:03.600 So that that's why there are three statements that print. 00:15:04.920 --> 00:15:09.960 There is stored, but we never used, a function called print lyrics. 00:15:09.960 --> 00:15:13.520 And it's got two statements in it, but we never used it. 00:15:13.520 --> 00:15:19.680 So the output of this is Hello Yo 7, and that's because we 00:15:19.680 --> 00:15:21.080 never actually invoked it. 00:15:21.080 --> 00:15:26.700 We had to say print lyrics parentheses or whatever to cause it to call this. 00:15:26.700 --> 00:15:29.260 Okay? That's just to emphasize that as it 00:15:29.260 --> 00:15:32.490 looks at it, it does not execute these lines. 00:15:33.990 --> 00:15:39.170 So once we've defined a function, once we have given it a name, given it 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. 00:15:44.550 --> 00:15:45.360 So now, 00:15:45.360 --> 00:15:48.800 our little example works a little better if we actually call our function. 00:15:51.360 --> 00:15:53.570 Python really doesn't care if you don't call your function. 00:15:53.570 --> 00:15:55.850 It's like you told me to make one, I made one. 00:15:55.850 --> 00:15:57.890 You didn't use it. There you go. 00:15:57.890 --> 00:16:02.860 But if you look at this one now, so here we go, x equals 5. 00:16:02.860 --> 00:16:05.530 print Hello, out comes Hello. 00:16:05.530 --> 00:16:06.310 Define. 00:16:06.310 --> 00:16:08.050 Nothing happens here. Nothing happens here. 00:16:08.050 --> 00:16:09.010 It's just remembering. 00:16:10.540 --> 00:16:12.870 Okay? Then it says, print Yo. 00:16:12.870 --> 00:16:16.434 Then it calls the function print lyrics, which sort of stops us here, 00:16:16.434 --> 00:16:20.700 runs these two lines of code. So out comes that and that. 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. 00:16:26.600 --> 00:16:28.976 That must mean that x is 7, and so out that comes. 00:16:28.976 --> 00:16:35.310 And so, again, it's on the first time through. 00:16:35.310 --> 00:16:36.560 No, go back, go back, go back. 00:16:37.830 --> 00:16:40.500 On the first time through, it doesn't print. 00:16:40.500 --> 00:16:41.554 But then when it hits this, 00:16:41.554 --> 00:16:42.680 it prints. 00:16:42.680 --> 00:16:44.530 You could say print lyrics several more times and 00:16:44.530 --> 00:16:46.700 it would run this as many times as it did, 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. 00:16:50.630 --> 00:16:55.150 So you can invoke this is the definition, let's clear this. 00:16:56.510 --> 00:17:03.320 This is the definition. This is the call or invoke. 00:17:04.020 --> 00:17:06.560 So we are invoking the function, we're calling the function, 00:17:06.560 --> 00:17:09.700 we're causing the function to execute. 00:17:09.700 --> 00:17:14.109 Here we're just causing the function to be looked at and defined, 00:17:14.109 --> 00:17:17.410 but not actually executed. Hope that's clear. 00:17:17.410 --> 00:17:23.410 Now, when we pass data into a function, and functions 00:17:23.410 --> 00:17:26.319 that don't take data are not as useful as they could be. 00:17:26.319 --> 00:17:28.240 There are plenty of things that do, times 00:17:28.240 --> 00:17:29.710 that you build a function doesn't take data. 00:17:29.710 --> 00:17:31.630 But the most interesting functions are the ones 00:17:31.630 --> 00:17:33.760 that you could hand them something to work on and 00:17:33.760 --> 00:17:36.780 they could do their work and then come back with whatever. 00:17:36.780 --> 00:17:41.790 So this max function is a good example of this, one that's taking an argument. 00:17:41.790 --> 00:17:44.085 We call the things in between the parentheses 00:17:44.085 --> 00:17:46.380 when we're invoking the function, 00:17:46.380 --> 00:17:50.740 we call the things in between the parentheses arguments, okay? 00:17:50.740 --> 00:17:52.680 So that's passing into the function. 00:17:52.680 --> 00:17:56.580 Feeding data into the function. So we put arguments in between them. 00:17:58.900 --> 00:18:03.000 So for example, here we have a little program. 00:18:03.000 --> 00:18:04.500 That 00:18:05.980 --> 00:18:09.270 that is, it's a function named greet, and now we are 00:18:09.270 --> 00:18:12.280 going to define this function and we're going to say, you know what? 00:18:12.280 --> 00:18:15.820 I would like to take a parameter, let's take a parameter. 00:18:15.820 --> 00:18:17.880 Let's have one parameter come in. 00:18:17.880 --> 00:18:19.540 And we need kind of a placeholder for that 00:18:19.540 --> 00:18:22.740 parameter, so within the function we're going to use lang. 00:18:22.740 --> 00:18:24.640 Now this isn't actually a real variable. 00:18:24.640 --> 00:18:28.130 It's kind of like a, it's a placeholder variable. 00:18:28.130 --> 00:18:31.070 So this first parameter, whatever it is, 00:18:31.070 --> 00:18:32.970 when it's called, is going to be lang. 00:18:32.970 --> 00:18:36.560 And so if that first parameter is equal to es, 00:18:36.620 --> 00:18:38.730 we're going to print Hola. 00:18:38.730 --> 00:18:42.760 And else if it's equal to fr we'll print Bonjour. 00:18:42.760 --> 00:18:44.480 And otherwise, we'll print Hello. 00:18:44.480 --> 00:18:48.950 So there's apparently three languages in the world, Spanish, French, and English. 00:18:48.950 --> 00:18:51.710 And if it's not Spanish or French, then it must be English. 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. 00:18:56.340 --> 00:18:59.160 So this is again just the definition and if you type this 00:18:59.160 --> 00:19:02.160 into the interactive thing it gives you this dot dot dot prompt. 00:19:02.160 --> 00:19:04.240 And so we now have this thing called greet 00:19:04.240 --> 00:19:07.820 and now we've extended Python to add our own function to Python. 00:19:07.820 --> 00:19:13.030 And now we can say greet en and so it runs this code 00:19:13.030 --> 00:19:18.630 except that en is lang and so that comes, and then it prints Hello. 00:19:18.630 --> 00:19:21.350 So out comes Hello. Now later we can say, oh, 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. 00:19:25.740 --> 00:19:32.300 So lang becomes, for this execution, es. And then, so it prints out Hola. 00:19:32.300 --> 00:19:39.070 And then the next execution, lang is fr. So it executes this three times but 00:19:39.070 --> 00:19:44.480 lang is different each time because we've passed in different parameters each time. 00:19:44.480 --> 00:19:47.130 So that's how we can kind of write general-purpose code 00:19:47.130 --> 00:19:52.610 inside the function and then reuse that general-purpose code in different ways. 00:19:52.610 --> 00:19:55.260 Okay? It's a real powerful, powerful mechanism 00:19:55.260 --> 00:19:57.310 that makes functions far more useful. 00:20:00.040 --> 00:20:05.170 Now, functions don't necessarily just have to do stuff. 00:20:05.170 --> 00:20:08.460 A real powerful mechanism in a function is what we call a return value. 00:20:11.270 --> 00:20:13.620 So a function can take its arguments, do some work. 00:20:13.620 --> 00:20:14.920 We've seen that. 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 00:20:20.550 --> 00:20:22.950 function, like we were calling max, 00:20:22.950 --> 00:20:26.560 it gives us back some value like the little w, okay? 00:20:26.560 --> 00:20:32.540 So here we're going to make a function called greet that takes no parameters. 00:20:32.540 --> 00:20:35.210 Doesn't take parameters, but it has another keyword. 00:20:35.210 --> 00:20:38.200 It's another reserved word in Python 00:20:38.200 --> 00:20:41.070 and whatever we put on this 00:20:41.070 --> 00:20:47.250 return statement shows up as the replacement in this expression. 00:20:47.250 --> 00:20:50.560 So, whatever greet is, it runs greet and 00:20:50.560 --> 00:20:53.290 then the return is kind of a residual value. 00:20:53.290 --> 00:20:55.890 So if we say print greet, comma, Glenn. 00:20:55.890 --> 00:20:58.190 it says Hello Glenn, because the return value 00:20:58.190 --> 00:21:01.045 for the greet function is the string Hello. 00:21:01.045 --> 00:21:03.160 And if we say greet Sally, 00:21:03.160 --> 00:21:04.970 it says Hello Sally. 00:21:04.970 --> 00:21:09.710 And so, and it's run the code twice and the return function, return value has 00:21:09.710 --> 00:21:14.100 been put in here instead. And so the Hello came there and the Hello came there, 00:21:14.100 --> 00:21:15.510 so we get the two lines. 00:21:15.510 --> 00:21:18.660 So, return is a statement that both terminates the 00:21:18.660 --> 00:21:22.520 execution of the function and defines the value of 00:21:22.520 --> 00:21:25.770 what will be replaced when the function call comes back, 00:21:25.770 --> 00:21:28.150 in the line that the function was called from. 00:21:29.980 --> 00:21:35.180 So here is a little smarter version of our greet function. 00:21:35.180 --> 00:21:40.340 It's very similar, it's called greet still, takes lang as a parameter. 00:21:40.340 --> 00:21:44.470 And if the language is es, then it returns the string Hola. 00:21:44.470 --> 00:21:46.760 If the language is French, it returns Bonjour. 00:21:46.760 --> 00:21:47.950 Otherwise, it returns Hello. 00:21:47.950 --> 00:21:49.570 So we're not actually doing the print, if you 00:21:49.570 --> 00:21:51.500 go back on the other slides, we were printing. 00:21:51.500 --> 00:21:55.620 But now we're just returning a string. Okay? 00:21:55.620 --> 00:22:00.880 And so now, I can call print greet, and pass en in, 00:22:00.880 --> 00:22:04.090 so then that runs the code once, with lang equal to en. 00:22:04.090 --> 00:22:06.880 And I get back Hello, and then comma, Glenn. 00:22:06.880 --> 00:22:09.470 Then I call it again and I pass es in. And then 00:22:09.470 --> 00:22:14.920 that time it returns, the return value here becomes Hola, a string Hola. 00:22:14.920 --> 00:22:19.870 Hola Sally. And then Michael, I'll pass in one more time. 00:22:19.870 --> 00:22:20.820 Lang is now fr, 00:22:20.820 --> 00:22:25.990 the string fr, and so it returns a Bonjour and 00:22:25.990 --> 00:22:31.620 so the, the residual that is here is Bonjour, and so out comes Bonjour Michael. 00:22:31.620 --> 00:22:33.800 So there is lot to this, right? 00:22:33.800 --> 00:22:34.910 You're passing stuff in. 00:22:34.910 --> 00:22:37.520 You have this kind of placeholder variable. 00:22:37.520 --> 00:22:42.020 And you have this return that sort of appears where it was called from. 00:22:42.020 --> 00:22:45.670 This goes in, does its work, it comes back, and there's sort of the 00:22:45.670 --> 00:22:47.410 residual value that sits here. 00:22:47.410 --> 00:22:50.940 You don't have to have a return in a function, but if you want to 00:22:50.940 --> 00:22:55.140 do something with the value, then you have to have a return in the function. 00:22:55.140 --> 00:22:58.320 We call the functions that produce values 00:22:58.320 --> 00:23:01.575 fruitful, and the other ones are called void. 00:23:01.575 --> 00:23:04.500 [LAUGH] So, that's a good name for them. 00:23:05.630 --> 00:23:07.800 So, to review sort of this, arguments, 00:23:07.800 --> 00:23:10.630 parameters, and results, if we look at max, 00:23:10.630 --> 00:23:15.960 the original thing, where it's looking for the largest, lexicographically 00:23:15.960 --> 00:23:21.950 largest letter, it looks Hello world is the argument that's passed in. 00:23:21.950 --> 00:23:25.420 We have this sort of formal parameter here called inp which is not really a 00:23:25.420 --> 00:23:28.430 variable, it just happens to refer to whatever is the 00:23:28.430 --> 00:23:32.250 first argument in any particular call. 00:23:32.250 --> 00:23:35.950 And then it does its little thing and runs loops and does all these things and 00:23:35.950 --> 00:23:41.180 at some point it returns w, so that the thing that comes out when the function 00:23:41.180 --> 00:23:47.020 quits that becomes the replacement value here is a lowercase w string. 00:23:47.020 --> 00:23:49.590 And then that is the w that goes over in the big. 00:23:49.590 --> 00:23:54.890 So the return is what defines what comes back here. 00:23:54.890 --> 00:23:58.850 Because you think of this as, it's looking at this. It suspends for the moment. 00:23:58.850 --> 00:24:00.300 It runs this code. 00:24:00.300 --> 00:24:00.890 It's holding. 00:24:00.890 --> 00:24:02.310 It's holding itself here. 00:24:02.310 --> 00:24:05.080 It's running this code and then it comes back to here. 00:24:06.220 --> 00:24:07.190 Okay? 00:24:07.190 --> 00:24:10.200 And the return value is what defines coming back. 00:24:11.260 --> 00:24:17.510 So, of course you can have more than one parameter and they are in order. 00:24:17.510 --> 00:24:18.820 So here we have an a and a b. 00:24:19.860 --> 00:24:22.390 These, the name of these things doesn't really matter. 00:24:22.390 --> 00:24:25.890 They're just relevant inside of the function definition. 00:24:25.890 --> 00:24:28.170 So we are going to add two numbers together 00:24:28.170 --> 00:24:31.860 by taking a + b, and then returning the sum. 00:24:31.860 --> 00:24:34.130 The added variable is just kind of local to this function. 00:24:34.130 --> 00:24:38.140 And now we can say, you know, addtwo, 3, comma, 5. 00:24:38.140 --> 00:24:40.310 And then this will come back as 8, and then 8 will get assigned 00:24:40.310 --> 00:24:42.990 into x, and so that will print out 8. 00:24:42.990 --> 00:24:45.340 And so you can have as many of these as you want 00:24:45.340 --> 00:24:49.275 and the order matters and there is a one-to-one correspondence. 00:24:49.275 --> 00:24:51.040 3 goes to a and 5 goes to b 00:24:51.040 --> 00:24:52.380 when the thing is called. 00:24:52.380 --> 00:24:56.410 And then the return value again comes back. 00:24:56.410 --> 00:25:01.080 Okay? So that's, sort of, arguments. 00:25:01.080 --> 00:25:04.850 And like I said, not all functions have to return values. 00:25:04.850 --> 00:25:07.980 We call them void functions when they don't return anything. 00:25:07.980 --> 00:25:10.860 It's totally fine for that to be the case. 00:25:12.490 --> 00:25:16.350 So at this point you might be thinking to yourself, okay, great, well I still 00:25:16.350 --> 00:25:18.320 don't quite get why to use functions. 00:25:18.320 --> 00:25:22.890 And in reality, in the first 10, 11 chapters of this book, other than 00:25:22.890 --> 00:25:26.470 using lots of functions, we're not really going to spend a lot 00:25:26.470 --> 00:25:30.500 of time making functions because most of our programs are going to kind of be 00:25:30.500 --> 00:25:33.650 that long and we're not going to do a lot reuse in the program. 00:25:33.650 --> 00:25:39.040 And there'll be a time when your programs become complex enough. 00:25:39.040 --> 00:25:41.450 You'll be like, oh, thank heaven for functions. 00:25:41.450 --> 00:25:44.580 I think it's premature to say you must use functions, even though 00:25:44.580 --> 00:25:47.860 there are some exercises that just say, hey, do this with a function. 00:25:47.860 --> 00:25:49.800 Just so you kind of get the understanding of a function. 00:25:50.880 --> 00:25:52.369 You will find soon enough, 00:25:53.550 --> 00:25:55.790 as your programs grow, you'll go like, oh, 00:25:55.790 --> 00:25:57.480 I keep doing this same thing over and over again. 00:25:57.480 --> 00:25:59.280 Let me pull it up into a function and pass 00:25:59.280 --> 00:26:02.630 a parameter in, have a return value, and away you go. 00:26:02.630 --> 00:26:04.560 Or, you might find that you're moving from 00:26:04.560 --> 00:26:06.480 one program to another and you have this common thing 00:26:06.480 --> 00:26:09.570 that you want to do, so you make yourself a library that you drag along. 00:26:09.570 --> 00:26:12.190 And we will do lots of libraries. 00:26:12.190 --> 00:26:14.910 The book in the second half does lots and lots of library 00:26:14.910 --> 00:26:18.880 stuff, doing things like parsing XML and this, that, and the other thing. 00:26:18.880 --> 00:26:24.130 So, so don't feel like you need to use functions on every 00:26:24.130 --> 00:26:27.930 assignment, because they're a natural thing when a program gets big enough. 00:26:27.930 --> 00:26:30.920 So, so just kind of understand them on a 00:26:30.920 --> 00:26:32.960 mechanical level, but 00:26:32.960 --> 00:26:34.930 it'll come to you at the right time 00:26:34.930 --> 00:26:38.000 when it's time to start building your own functions. 00:26:38.000 --> 00:26:40.850 So in this class, we kind of, you know, talked about functions. 00:26:40.850 --> 00:26:41.730 Just got you started. 00:26:41.730 --> 00:26:45.180 Talked about parameters, talked about built-in functions, 00:26:45.180 --> 00:26:50.290 talked about return values, the store and reuse pattern. 00:26:50.290 --> 00:26:52.630 So, the problems at the end of the 00:26:52.630 --> 00:26:57.100 chapter for this particular chapter are relatively straightforward, 00:26:57.100 --> 00:27:00.790 in that, like I said, we don't have a real strong 00:27:00.790 --> 00:27:05.630 need to do functions yet in this class because the programs aren't large enough. 00:27:05.630 --> 00:27:08.017 But I just said okay, take, take one 00:27:08.017 --> 00:27:12.060 of your previous assignments and refactor the code 00:27:12.060 --> 00:27:16.880 so that at the top there is a def computepay, and you put like the if 00:27:16.880 --> 00:27:19.830 and whatever in here, and then later on 00:27:19.830 --> 00:27:22.270 you do your code and then you call computepay. 00:27:22.270 --> 00:27:24.200 So you took code that you already had, you 00:27:24.200 --> 00:27:27.140 move it up into a function, and make a function. 00:27:27.140 --> 00:27:29.160 And I've also online got sort of a 00:27:29.160 --> 00:27:31.810 sample of this, because it's, it's a little complex. 00:27:31.810 --> 00:27:36.820 And so you should be able to find, on Python Learn or on the course site, 00:27:38.140 --> 00:27:39.760 you should be able to find a good example, 00:27:39.760 --> 00:27:41.670 because I really want you to sort of get this. 00:27:43.320 --> 00:27:45.290 Like I said, there will come a time 00:27:45.290 --> 00:27:47.740 when functions will make the most sense to you. 00:27:47.740 --> 00:27:51.990 But coming up next, of course, is Chapter Five, and that's loops. 00:27:51.990 --> 00:27:55.110 And loops are going to rock the house, And so we really, that's our 00:27:55.110 --> 00:28:00.530 fourth major pattern is loops and and I'm looking forward to it. 00:28:00.530 --> 00:28:02.620 So we'll we'll see you at the next lecture.