[Script Info] Title: [Events] Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text Dialogue: 0,0:00:00.24,0:00:02.74,Default,,0000,0000,0000,,Hello, and welcome to Chapter Four, Dialogue: 0,0:00:02.74,0:00:05.89,Default,,0000,0000,0000,,Functions, in the book Python for\NInformatics. Dialogue: 0,0:00:06.98,0:00:09.56,Default,,0000,0000,0000,,As always, these slides and this audio and Dialogue: 0,0:00:09.56,0:00:12.99,Default,,0000,0000,0000,,this video are copyright Creative\NCommons Attribution. Dialogue: 0,0:00:14.61,0:00:19.22,Default,,0000,0000,0000,,Now we are to the point, you know, Chapter\NFour, we're sort of well into the class. Dialogue: 0,0:00:19.22,0:00:23.56,Default,,0000,0000,0000,,So I figure I should introduce myself a\Nlittle bit, let you know a little bit. Dialogue: 0,0:00:24.93,0:00:27.27,Default,,0000,0000,0000,,As I said before, I think in the beginning, Dialogue: 0,0:00:27.27,0:00:30.72,Default,,0000,0000,0000,,we're tape, I'm taping this in a Dialogue: 0,0:00:30.72,0:00:35.74,Default,,0000,0000,0000,,wonderful building at the University\Nof Michigan called the North Quad. Dialogue: 0,0:00:35.74,0:00:39.70,Default,,0000,0000,0000,,It's a relatively new building, \Nit's got some Dialogue: 0,0:00:39.70,0:00:42.47,Default,,0000,0000,0000,,residential sections and some academic\Nsections and some Dialogue: 0,0:00:42.47,0:00:44.92,Default,,0000,0000,0000,,classrooms, and one of the classrooms that Dialogue: 0,0:00:44.92,0:00:49.22,Default,,0000,0000,0000,,I typically teach in is actually \N2255 North Quad. Dialogue: 0,0:00:49.22,0:00:50.70,Default,,0000,0000,0000,,It's a really beautiful room Dialogue: 0,0:00:50.70,0:00:54.19,Default,,0000,0000,0000,,with great ways for people to interact,\Nand so sometimes Dialogue: 0,0:00:54.19,0:00:58.46,Default,,0000,0000,0000,,I'm teaching, you know, little tiny\NDr. Chuck down here. Dialogue: 0,0:00:58.46,0:00:59.83,Default,,0000,0000,0000,,With a smile on the face. Dialogue: 0,0:00:59.83,0:01:03.60,Default,,0000,0000,0000,,And sometimes my students are\Ntaking me on, taking my Dialogue: 0,0:01:03.60,0:01:08.72,Default,,0000,0000,0000,,classes on campus and sometimes students\Nare watching me through lecture. Dialogue: 0,0:01:10.83,0:01:14.11,Default,,0000,0000,0000,,And so this building is really \Nbeautiful and if Dialogue: 0,0:01:14.11,0:01:15.94,Default,,0000,0000,0000,,you ever get a chance to come to Ann Arbor Dialogue: 0,0:01:15.94,0:01:19.10,Default,,0000,0000,0000,,and look at it, maybe walk through it, \Nit's really, it's really quite nice. Dialogue: 0,0:01:19.10,0:01:20.91,Default,,0000,0000,0000,,One of the things I like about it is Dialogue: 0,0:01:20.91,0:01:27.11,Default,,0000,0000,0000,,that I think it's really highly \Ninspired by Harry Potter. Dialogue: 0,0:01:27.11,0:01:29.94,Default,,0000,0000,0000,,The kind of, of course, Oxford and\NCambridge are Dialogue: 0,0:01:29.94,0:01:34.79,Default,,0000,0000,0000,,the real inspiration for Harry Potter, \Nbut our, our cafeteria, Dialogue: 0,0:01:34.79,0:01:38.65,Default,,0000,0000,0000,,for example, it kind of looks \Nlike the four tables Dialogue: 0,0:01:38.65,0:01:40.99,Default,,0000,0000,0000,,in Hogwarts and you can kind of \Nimagine a snowy owl Dialogue: 0,0:01:40.99,0:01:46.97,Default,,0000,0000,0000,,flying around and a Sorting Hat at the,\Nat the front sorting people. Dialogue: 0,0:01:46.97,0:01:53.11,Default,,0000,0000,0000,,And so the nickname, the nickname for the\Nplace is Quadwarts, because it's North Quad Dialogue: 0,0:01:53.11,0:01:59.82,Default,,0000,0000,0000,,Quadwarts, that's like Hogwarts and\NNorth Quad kind of jammed together. Dialogue: 0,0:01:59.82,0:02:01.99,Default,,0000,0000,0000,,And of course, given that we sort of think Dialogue: 0,0:02:01.99,0:02:06.82,Default,,0000,0000,0000,,of ourselves a little bit as Harry Potter, Dialogue: 0,0:02:06.82,0:02:11.44,Default,,0000,0000,0000,,people when they first come in September\Noften sort of decide Dialogue: 0,0:02:11.44,0:02:14.07,Default,,0000,0000,0000,,to sort themselves and a few years back Dialogue: 0,0:02:14.07,0:02:17.48,Default,,0000,0000,0000,,when we first started the building\Nthe students Dialogue: 0,0:02:17.48,0:02:21.99,Default,,0000,0000,0000,,decided that I did not get to \Nbe in Gryffindor. Dialogue: 0,0:02:21.99,0:02:24.42,Default,,0000,0000,0000,,As a matter of fact, it's probably \Ntime for me to to Dialogue: 0,0:02:24.42,0:02:27.93,Default,,0000,0000,0000,,show you who I am, and who \NI've been sorted to be. Dialogue: 0,0:02:27.93,0:02:31.60,Default,,0000,0000,0000,,So the students decided that \NI couldn't be in Griffindor. Dialogue: 0,0:02:31.60,0:02:33.98,Default,,0000,0000,0000,,That I had to be in Slitherin, Dialogue: 0,0:02:33.98,0:02:36.62,Default,,0000,0000,0000,,and that's because of\Nmy name, Charles Severance. Dialogue: 0,0:02:36.62,0:02:38.18,Default,,0000,0000,0000,,And sever is snake. Dialogue: 0,0:02:38.18,0:02:40.48,Default,,0000,0000,0000,,What's even cooler, of course, Dialogue: 0,0:02:40.48,0:02:46.11,Default,,0000,0000,0000,,is given that I teach Python, \NSlitherin's house is Dialogue: 0,0:02:46.11,0:02:50.30,Default,,0000,0000,0000,,a snake, right?\NSo it makes a lot of sense, I even have Dialogue: 0,0:02:50.30,0:02:56.15,Default,,0000,0000,0000,,this really fancy Slitherin teacup that I\Nuse to drink tea during lectures. Dialogue: 0,0:02:56.15,0:02:57.16,Default,,0000,0000,0000,,Sometimes I drink coffee, and Dialogue: 0,0:02:57.16,0:03:00.20,Default,,0000,0000,0000,,sometimes I drink tea.\NOh wow, this thing itches. Dialogue: 0,0:03:00.20,0:03:02.19,Default,,0000,0000,0000,,So let me just get rid of it. Dialogue: 0,0:03:02.19,0:03:04.06,Default,,0000,0000,0000,,If I had any hair, that would \Nmess my hair up. Dialogue: 0,0:03:04.06,0:03:06.15,Default,,0000,0000,0000,,So let me get rid of this for the \Nrest of the lecture. Dialogue: 0,0:03:08.59,0:03:13.30,Default,,0000,0000,0000,,So there I am, okay.\NEnough of that, back to, Dialogue: 0,0:03:13.30,0:03:17.20,Default,,0000,0000,0000,,back to Dr. Chuck.\N[CLAP] Dialogue: 0,0:03:17.20,0:03:24.94,Default,,0000,0000,0000,,So, with that sort of brief, brief\Ninterlude, the topic of Dialogue: 0,0:03:24.94,0:03:28.78,Default,,0000,0000,0000,,the actual topic of this lecture is\NFunctions. And so storing and Dialogue: 0,0:03:28.78,0:03:33.83,Default,,0000,0000,0000,,reusing is basically an idea that Dialogue: 0,0:03:33.83,0:03:37.69,Default,,0000,0000,0000,,we will often have a series of \Nsteps that we will Dialogue: 0,0:03:37.69,0:03:42.11,Default,,0000,0000,0000,,want to use over and over in a program,\Nincreasingly complex. Dialogue: 0,0:03:42.11,0:03:45.49,Default,,0000,0000,0000,,The things that we'll use in this lecture\Nare kind of silly because Dialogue: 0,0:03:45.49,0:03:48.16,Default,,0000,0000,0000,,I have to keep them short so the slides\Ndon't get too long. Dialogue: 0,0:03:48.16,0:03:53.76,Default,,0000,0000,0000,,But a good example of, you know, the kind\Nof work is maybe I am going to use Dialogue: 0,0:03:53.76,0:03:59.29,Default,,0000,0000,0000,,Google's geocoding service and I am\Ngoing to send some unstructured Dialogue: 0,0:03:59.29,0:04:02.62,Default,,0000,0000,0000,,data and get a GPS coordinate back. Dialogue: 0,0:04:02.62,0:04:04.80,Default,,0000,0000,0000,,And that's a service that I want \Nto call and it would Dialogue: 0,0:04:04.80,0:04:07.37,Default,,0000,0000,0000,,maybe be about this much lines of, this\Nmany lines of code. Dialogue: 0,0:04:07.37,0:04:09.25,Default,,0000,0000,0000,,And I'm going to want to do that \Nall over the place. Dialogue: 0,0:04:09.25,0:04:13.89,Default,,0000,0000,0000,,So do I want to put this many lines\Nof code 40 places in my program? Dialogue: 0,0:04:13.89,0:04:15.38,Default,,0000,0000,0000,,Or do I want to put it one place and Dialogue: 0,0:04:15.38,0:04:18.06,Default,,0000,0000,0000,,then call it in the various places \Nthat I need it? Dialogue: 0,0:04:18.06,0:04:20.92,Default,,0000,0000,0000,,And so that's why I call it the store and\Nthe reuse function. Dialogue: 0,0:04:22.22,0:04:25.28,Default,,0000,0000,0000,,So if we take a look at the \Nsimple syntax here. Dialogue: 0,0:04:27.65,0:04:31.79,Default,,0000,0000,0000,,These things are called functions and in some\Nlanguages it's called subprograms, Dialogue: 0,0:04:31.79,0:04:36.97,Default,,0000,0000,0000,,but we call them functions in Python.\NAnd the keyword Dialogue: 0,0:04:36.97,0:04:40.32,Default,,0000,0000,0000,,that we're really going to focus on is \Ndef, which stands for Dialogue: 0,0:04:40.32,0:04:46.81,Default,,0000,0000,0000,,define, and what happens here is \Nwhen Python sees this def keyword Dialogue: 0,0:04:46.81,0:04:49.03,Default,,0000,0000,0000,,it actually doesn't run the code. Dialogue: 0,0:04:50.23,0:04:52.69,Default,,0000,0000,0000,,It says, oh, you're going to make a\Nfunction and you're Dialogue: 0,0:04:52.69,0:04:57.18,Default,,0000,0000,0000,,going to kind of turn on a recorder and\Nstart recording this code. Dialogue: 0,0:04:57.18,0:04:58.70,Default,,0000,0000,0000,,So it has a colon at the end of it. Dialogue: 0,0:04:58.70,0:05:01.06,Default,,0000,0000,0000,,So it has an indented block afterwards. Dialogue: 0,0:05:01.06,0:05:04.05,Default,,0000,0000,0000,,And so the indented block becomes\Nrecorded. Dialogue: 0,0:05:05.23,0:05:07.44,Default,,0000,0000,0000,,So instead of running the code, \Nlike if we just Dialogue: 0,0:05:07.44,0:05:09.87,Default,,0000,0000,0000,,put print hello and print fun, \Nit would run it. Dialogue: 0,0:05:09.87,0:05:14.55,Default,,0000,0000,0000,,Instead it says, hey don't run it right\Nnow, name it hello. Dialogue: 0,0:05:14.55,0:05:15.90,Default,,0000,0000,0000,,We give it a name. Dialogue: 0,0:05:15.90,0:05:18.22,Default,,0000,0000,0000,,It's kind of like a variable.\NWe choose the name. Dialogue: 0,0:05:18.22,0:05:21.29,Default,,0000,0000,0000,,We've chosen hello as the name of this. Dialogue: 0,0:05:21.29,0:05:23.77,Default,,0000,0000,0000,,Define it as hello, have it have these two Dialogue: 0,0:05:23.77,0:05:27.13,Default,,0000,0000,0000,,lines of Python in it, and we'll \Nuse it later, okay? Dialogue: 0,0:05:27.13,0:05:29.73,Default,,0000,0000,0000,,And so that's the function\Ndefinition. Dialogue: 0,0:05:29.73,0:05:31.21,Default,,0000,0000,0000,,That's the store phase. Dialogue: 0,0:05:31.21,0:05:34.52,Default,,0000,0000,0000,,That is it's sort of like, it doesn't\Nreally run those lines. Dialogue: 0,0:05:34.52,0:05:37.22,Default,,0000,0000,0000,,It sort of makes a variable called hello Dialogue: 0,0:05:37.22,0:05:39.73,Default,,0000,0000,0000,,that actually contains Python code Dialogue: 0,0:05:39.73,0:05:43.20,Default,,0000,0000,0000,,rather than containing like 12 or a \Nstring or something like that Dialogue: 0,0:05:43.20,0:05:45.22,Default,,0000,0000,0000,,that we've worked with before. Dialogue: 0,0:05:45.22,0:05:50.25,Default,,0000,0000,0000,,So this is the store part, and then the \Nreuse part is, we then have extended Python. Dialogue: 0,0:05:50.25,0:05:56.61,Default,,0000,0000,0000,,We now can call our bit of code. So we say,\Nhello, hello name is what we came up with, Dialogue: 0,0:05:56.61,0:06:00.24,Default,,0000,0000,0000,,parentheses, and then that says,\Nremember that code that I Dialogue: 0,0:06:00.24,0:06:02.70,Default,,0000,0000,0000,,put in there under the name hello? Dialogue: 0,0:06:02.70,0:06:05.87,Default,,0000,0000,0000,,Run it now and, so, so, so if I Dialogue: 0,0:06:05.87,0:06:08.65,Default,,0000,0000,0000,,start looking at that and then \Nit just continues. Dialogue: 0,0:06:08.65,0:06:11.47,Default,,0000,0000,0000,,So let me kind of clear this \Nand start over again. Dialogue: 0,0:06:11.47,0:06:14.65,Default,,0000,0000,0000,,And, so if I watch what Python does from\Nthe beginning as Dialogue: 0,0:06:14.65,0:06:18.11,Default,,0000,0000,0000,,it reads here and it goes oh, you're\Ndefining a function named hello. Dialogue: 0,0:06:18.11,0:06:22.11,Default,,0000,0000,0000,,Great, I will sort of remember, remember. Dialogue: 0,0:06:22.11,0:06:24.65,Default,,0000,0000,0000,,I got that remembered for you, let's\Ncontinue on. Dialogue: 0,0:06:24.65,0:06:25.72,Default,,0000,0000,0000,,Oh, hello! Dialogue: 0,0:06:25.72,0:06:27.47,Default,,0000,0000,0000,,You want me to run that stuff that Dialogue: 0,0:06:27.47,0:06:30.29,Default,,0000,0000,0000,,you just got done storing under \Nthe name hello. Dialogue: 0,0:06:30.29,0:06:34.29,Default,,0000,0000,0000,,So then it kind of goes and runs it and\Nout comes Hello Fun. Dialogue: 0,0:06:34.29,0:06:38.75,Default,,0000,0000,0000,,Then after that it runs to the print \Nand out comes print Zip. Dialogue: 0,0:06:38.75,0:06:39.60,Default,,0000,0000,0000,,And we say, you know what? Dialogue: 0,0:06:39.60,0:06:41.14,Default,,0000,0000,0000,,I want to reuse that again. Dialogue: 0,0:06:41.14,0:06:44.15,Default,,0000,0000,0000,,I stored it once, I can reuse it as many\Ntimes as I want. Dialogue: 0,0:06:44.15,0:06:48.83,Default,,0000,0000,0000,,And now, hello, and then these \Ntwo lines of code run a second time. Dialogue: 0,0:06:48.83,0:06:51.93,Default,,0000,0000,0000,,So we stored them once, gave them \Na name, and then Dialogue: 0,0:06:51.93,0:06:55.72,Default,,0000,0000,0000,,ran them twice, in the context of \Nwherever it is we wanted. Dialogue: 0,0:06:55.72,0:07:00.03,Default,,0000,0000,0000,,Now this is not sort of a profound Dialogue: 0,0:07:00.03,0:07:02.93,Default,,0000,0000,0000,,a profound reason to use it in this. Dialogue: 0,0:07:02.93,0:07:04.69,Default,,0000,0000,0000,,I'm just trying to give you \Nthe notion that there's a Dialogue: 0,0:07:04.69,0:07:08.97,Default,,0000,0000,0000,,way to store and name code that \Nthen you can retrieve later. Dialogue: 0,0:07:08.97,0:07:10.42,Default,,0000,0000,0000,,That's really what's going on here. Dialogue: 0,0:07:11.57,0:07:13.47,Default,,0000,0000,0000,,There's two kind of functions \Ninside of Python, and Dialogue: 0,0:07:13.47,0:07:16.81,Default,,0000,0000,0000,,we've actually been using them almost from\Nthe very first lecture. Dialogue: 0,0:07:16.81,0:07:19.47,Default,,0000,0000,0000,,And that is there are built-in functions\Nthat Python provides Dialogue: 0,0:07:19.47,0:07:24.87,Default,,0000,0000,0000,,to us like float, raw_input, int, \Nthose kinds of functions. Dialogue: 0,0:07:24.87,0:07:28.15,Default,,0000,0000,0000,,Those are just part of Python but \Nwe call them as functions. Dialogue: 0,0:07:28.15,0:07:29.84,Default,,0000,0000,0000,,The difference is we don't write them. Dialogue: 0,0:07:29.84,0:07:32.86,Default,,0000,0000,0000,,And then there's user-defined functions,\Nfunctions that we write. Dialogue: 0,0:07:32.86,0:07:37.49,Default,,0000,0000,0000,,Functions that create functionality \Nthat we want to make use of. Dialogue: 0,0:07:37.49,0:07:42.82,Default,,0000,0000,0000,,Like encapsulating the ability to compute\Npay for time and a half for overtime. Dialogue: 0,0:07:42.82,0:07:45.67,Default,,0000,0000,0000,,And so, we name these things and we Dialogue: 0,0:07:45.67,0:07:48.13,Default,,0000,0000,0000,,treat them as new reserved words that\Nwe've created. Dialogue: 0,0:07:48.13,0:07:50.06,Default,,0000,0000,0000,,They're kind of an extension \Nto the language Dialogue: 0,0:07:50.06,0:07:52.56,Default,,0000,0000,0000,,as it were. Dialogue: 0,0:07:53.56,0:07:58.76,Default,,0000,0000,0000,,So when we're coming along, we define a\Nfunction with the def keyword, right? Dialogue: 0,0:07:58.76,0:07:59.68,Default,,0000,0000,0000,,The def keyword Dialogue: 0,0:08:00.76,0:08:04.18,Default,,0000,0000,0000,,is a reserved word. It's one of the many\Nreserved words back in Chapter One Dialogue: 0,0:08:04.18,0:08:08.52,Default,,0000,0000,0000,,that we talked about, and it indicates to\NPython the beginning of a function. Dialogue: 0,0:08:08.52,0:08:12.70,Default,,0000,0000,0000,,We define it, and then when we call it,\Nwhich is called invoking, Dialogue: 0,0:08:12.70,0:08:15.60,Default,,0000,0000,0000,,it's like, we're building it,\Nand then we're invoking it. Dialogue: 0,0:08:15.60,0:08:18.46,Default,,0000,0000,0000,,And you can build it once, and then invoke\Nit many, many times. Dialogue: 0,0:08:20.25,0:08:24.75,Default,,0000,0000,0000,,So for example, here is a built-in\Nfunction called max that Dialogue: 0,0:08:24.75,0:08:27.28,Default,,0000,0000,0000,,finds the largest character, the sort Dialogue: 0,0:08:27.28,0:08:31.65,Default,,0000,0000,0000,,of lexicographically largest character,\Nin a string. Dialogue: 0,0:08:31.65,0:08:35.71,Default,,0000,0000,0000,,And so it's like okay, tell me \Nthe maximum character. Dialogue: 0,0:08:35.71,0:08:37.41,Default,,0000,0000,0000,,And so max is not some code that we've Dialogue: 0,0:08:37.41,0:08:40.79,Default,,0000,0000,0000,,written but we are invoking a \Nfunction here and we're Dialogue: 0,0:08:40.79,0:08:43.39,Default,,0000,0000,0000,,passing in an argument to that. Dialogue: 0,0:08:43.39,0:08:47.82,Default,,0000,0000,0000,,So the argument is this stuff \Nin between the parentheses. Dialogue: 0,0:08:47.82,0:08:51.36,Default,,0000,0000,0000,,So the max function can find the maximum\Nof many different things. Dialogue: 0,0:08:51.36,0:08:54.73,Default,,0000,0000,0000,,At this moment we want it to find the\Nmaximum of that particular string. Dialogue: 0,0:08:54.73,0:08:56.37,Default,,0000,0000,0000,,The highest character Dialogue: 0,0:08:56.37,0:09:00.72,Default,,0000,0000,0000,,in that particular string.\NSo, this is a left, a right-hand side of Dialogue: 0,0:09:00.72,0:09:05.23,Default,,0000,0000,0000,,an assignment statement too.\NSo that has to be evaluated to a value. Dialogue: 0,0:09:06.38,0:09:07.74,Default,,0000,0000,0000,,So it goes into the function, Dialogue: 0,0:09:07.74,0:09:10.23,Default,,0000,0000,0000,,does whatever things the function \Nwants to do, Dialogue: 0,0:09:10.23,0:09:12.48,Default,,0000,0000,0000,,and then the function \Ngives us back a value Dialogue: 0,0:09:12.48,0:09:17.11,Default,,0000,0000,0000,,that becomes the value for \Nmax parentheses Hello world. Dialogue: 0,0:09:17.11,0:09:20.39,Default,,0000,0000,0000,,And that value in this case is \Nthe letter w, okay? Dialogue: 0,0:09:20.39,0:09:22.41,Default,,0000,0000,0000,,Because the letter w was \Ndecided to be Dialogue: 0,0:09:22.41,0:09:24.71,Default,,0000,0000,0000,,the highest letter and that's \Nwhat max gives us back. Dialogue: 0,0:09:26.12,0:09:30.53,Default,,0000,0000,0000,,And then when we're done with that,\Nthen that w ends up being assigned. Dialogue: 0,0:09:30.53,0:09:31.85,Default,,0000,0000,0000,,The assignment statement completes. Dialogue: 0,0:09:31.85,0:09:34.48,Default,,0000,0000,0000,,And so you can think of the \Nfunction evaluation Dialogue: 0,0:09:34.48,0:09:38.76,Default,,0000,0000,0000,,as happening as part of the right-hand\Nside expression calculation. Dialogue: 0,0:09:38.76,0:09:40.91,Default,,0000,0000,0000,,There could a plus here and other stuff Dialogue: 0,0:09:40.91,0:09:43.02,Default,,0000,0000,0000,,and it's just, at some point, \Na big expression. Dialogue: 0,0:09:43.02,0:09:46.30,Default,,0000,0000,0000,,And this one, it's a simple expression\Nwith just one function call. Dialogue: 0,0:09:48.53,0:09:50.83,Default,,0000,0000,0000,,Now if we look at this, there is some\Ncode somewhere. Dialogue: 0,0:09:50.83,0:09:52.95,Default,,0000,0000,0000,,Somebody wrote some code. Dialogue: 0,0:09:52.95,0:09:54.50,Default,,0000,0000,0000,,It's part of Python. Dialogue: 0,0:09:54.50,0:09:57.98,Default,,0000,0000,0000,,You didn't write it.\NThere's a max function somewhere. Dialogue: 0,0:09:57.98,0:09:59.98,Default,,0000,0000,0000,,And you can think of a function \Nas having some input. Dialogue: 0,0:09:59.98,0:10:02.17,Default,,0000,0000,0000,,It's kind of like a program, that's why Dialogue: 0,0:10:02.17,0:10:04.92,Default,,0000,0000,0000,,some languages call these things\Nsubprograms. Dialogue: 0,0:10:04.92,0:10:08.43,Default,,0000,0000,0000,,Because they have an input, they do some\Nkind of useful works, whatever Dialogue: 0,0:10:08.43,0:10:12.81,Default,,0000,0000,0000,,that useful work happens to be, and then\Nthey produce some kind of an output. Dialogue: 0,0:10:12.81,0:10:13.72,Default,,0000,0000,0000,,Right? So Hello world Dialogue: 0,0:10:13.72,0:10:16.74,Default,,0000,0000,0000,,is the input, a string, the\Narguments, the thing we're passing in. Dialogue: 0,0:10:16.74,0:10:20.83,Default,,0000,0000,0000,,Hello world is what's being\Npassed in to the function. Dialogue: 0,0:10:20.83,0:10:25.45,Default,,0000,0000,0000,,The function is running and then something\Ncomes back and is sent back. Dialogue: 0,0:10:25.45,0:10:29.54,Default,,0000,0000,0000,,So it has input, processing, and output. Dialogue: 0,0:10:29.54,0:10:32.14,Default,,0000,0000,0000,,Input, processing, and output. \NSo that's how a function. Dialogue: 0,0:10:32.14,0:10:35.06,Default,,0000,0000,0000,,Some stored code, whether we \Nwrote it or not, Dialogue: 0,0:10:35.06,0:10:37.69,Default,,0000,0000,0000,,they work the same when we call Dialogue: 0,0:10:37.69,0:10:38.87,Default,,0000,0000,0000,,functions, right? Dialogue: 0,0:10:38.87,0:10:43.60,Default,,0000,0000,0000,,So you could think of this as, somewhere\Ninside of the Python library Dialogue: 0,0:10:43.60,0:10:47.47,Default,,0000,0000,0000,,is some code that maybe has a little \Ndef in there, and the name, Dialogue: 0,0:10:47.47,0:10:51.37,Default,,0000,0000,0000,,they named the function max, and it \Ntakes a single parameter. Dialogue: 0,0:10:51.37,0:10:54.06,Default,,0000,0000,0000,,And it does some blah, blah, blah, blah, Dialogue: 0,0:10:54.06,0:10:58.98,Default,,0000,0000,0000,,loopy blah, blah stuff, whatever \Nmax wants to do. Dialogue: 0,0:10:58.98,0:11:00.68,Default,,0000,0000,0000,,Whatever we need max to do based on Dialogue: 0,0:11:00.68,0:11:03.46,Default,,0000,0000,0000,,the specifications that max is \Nsupposed to support. Dialogue: 0,0:11:03.46,0:11:04.22,Default,,0000,0000,0000,,But somewhere Dialogue: 0,0:11:04.22,0:11:06.80,Default,,0000,0000,0000,,there is code inside of Python Dialogue: 0,0:11:06.80,0:11:09.25,Default,,0000,0000,0000,,that actually represents the \Nfunction definition. Dialogue: 0,0:11:09.25,0:11:11.98,Default,,0000,0000,0000,,It's a built-in function because it \Ncomes with Python Dialogue: 0,0:11:11.98,0:11:14.84,Default,,0000,0000,0000,,and we didn't have to do \Nanything to add it. Dialogue: 0,0:11:14.84,0:11:17.63,Default,,0000,0000,0000,,So some common built-in functions \Nthat we have Dialogue: 0,0:11:17.63,0:11:22.36,Default,,0000,0000,0000,,been using all along, good examples \Nare the float, which Dialogue: 0,0:11:22.36,0:11:25.50,Default,,0000,0000,0000,,takes as input anything and Dialogue: 0,0:11:25.50,0:11:29.25,Default,,0000,0000,0000,,returns you a floating\Npoint number version of that. Dialogue: 0,0:11:29.25,0:11:32.12,Default,,0000,0000,0000,,Type, which takes a parameter \Nof a variable or a constant Dialogue: 0,0:11:32.12,0:11:35.25,Default,,0000,0000,0000,,and says, what is the type of this. Dialogue: 0,0:11:35.25,0:11:37.73,Default,,0000,0000,0000,,Float, again converting. Dialogue: 0,0:11:37.73,0:11:39.16,Default,,0000,0000,0000,,Type, again, and float. Dialogue: 0,0:11:39.16,0:11:42.10,Default,,0000,0000,0000,,So these are all things that, we've been\Ncalling functions all along. Dialogue: 0,0:11:42.10,0:11:45.66,Default,,0000,0000,0000,,And it passes the input value \Ninto the function, the Dialogue: 0,0:11:45.66,0:11:49.09,Default,,0000,0000,0000,,function runs and then gives\Nus back a return value Dialogue: 0,0:11:49.09,0:11:54.03,Default,,0000,0000,0000,,which then participates in the rest of the\Nexpression on the right-hand side. Dialogue: 0,0:11:54.03,0:11:56.31,Default,,0000,0000,0000,,You can think of it's pausing the calculation Dialogue: 0,0:11:56.31,0:11:59.88,Default,,0000,0000,0000,,on the right-hand side, calling the function, Dialogue: 0,0:11:59.88,0:12:01.48,Default,,0000,0000,0000,,getting the result of the function back, Dialogue: 0,0:12:01.48,0:12:04.07,Default,,0000,0000,0000,,and then continuing the evaluation of the\Nright-hand side. Dialogue: 0,0:12:04.07,0:12:08.35,Default,,0000,0000,0000,,Then coming up with whatever value and\Nthen printing that value out. Dialogue: 0,0:12:08.35,0:12:13.04,Default,,0000,0000,0000,,Okay?\NAnother thing that we've done Dialogue: 0,0:12:13.04,0:12:15.04,Default,,0000,0000,0000,,is we've done string conversions.\NRight? Dialogue: 0,0:12:15.04,0:12:18.49,Default,,0000,0000,0000,,So we've converted, in this case, \Na string to an integer. Dialogue: 0,0:12:18.49,0:12:20.18,Default,,0000,0000,0000,,And asked what type it is. Dialogue: 0,0:12:20.18,0:12:23.86,Default,,0000,0000,0000,,We've converted a string to an integer.\NSo, int converts its argument, Dialogue: 0,0:12:23.86,0:12:26.57,Default,,0000,0000,0000,,whatever that happens to be, \Ninto an integer. Dialogue: 0,0:12:26.57,0:12:27.97,Default,,0000,0000,0000,,So that's just some of the built-in Dialogue: 0,0:12:27.97,0:12:31.43,Default,,0000,0000,0000,,functions that we have talked about\Nso far. Dialogue: 0,0:12:31.43,0:12:33.59,Default,,0000,0000,0000,,Now, this becomes more interesting Dialogue: 0,0:12:36.82,0:12:39.18,Default,,0000,0000,0000,,when we can make our own, own functions. Dialogue: 0,0:12:39.18,0:12:44.33,Default,,0000,0000,0000,,Oops, there goes my teabag right in the\Nmiddle of the thing. Dialogue: 0,0:12:45.43,0:12:49.81,Default,,0000,0000,0000,,Got to take the teabag out, I think it's,\Nwhoa, hang on, be right back. Dialogue: 0,0:12:56.24,0:13:02.72,Default,,0000,0000,0000,,Teabag, okay, there's my tea.\NSo, so we want to make a new function. Dialogue: 0,0:13:02.72,0:13:06.13,Default,,0000,0000,0000,,Like I said in the other example,\Nwe use the def keyword, Dialogue: 0,0:13:08.62,0:13:12.18,Default,,0000,0000,0000,,the def keyword here, and then we have\Nsome indented bit. Dialogue: 0,0:13:12.18,0:13:16.14,Default,,0000,0000,0000,,We create a name for it and then \Nhave some parentheses. Dialogue: 0,0:13:16.14,0:13:19.21,Default,,0000,0000,0000,,These parentheses will later tell \Nthe inputs Dialogue: 0,0:13:19.21,0:13:20.44,Default,,0000,0000,0000,,that we're going to pass in, but this Dialogue: 0,0:13:20.44,0:13:24.65,Default,,0000,0000,0000,,function has no input, so we just go \Nparenthesis, parenthesis and then the Dialogue: 0,0:13:24.65,0:13:27.68,Default,,0000,0000,0000,,all-important colon character which \Nindicates the beginning of an Dialogue: 0,0:13:27.68,0:13:32.36,Default,,0000,0000,0000,,indented block of Python, that then is, \Nthis detects the function. Dialogue: 0,0:13:32.36,0:13:34.94,Default,,0000,0000,0000,,So, it's important to remember Dialogue: 0,0:13:34.94,0:13:37.69,Default,,0000,0000,0000,,that while this is executing\Nwhen Python first Dialogue: 0,0:13:37.69,0:13:40.86,Default,,0000,0000,0000,,looks at this, it doesn't run \Nthese lines of code. Dialogue: 0,0:13:40.86,0:13:44.84,Default,,0000,0000,0000,,It just remembers them, and names them\Nprint lyrics. Dialogue: 0,0:13:44.84,0:13:48.80,Default,,0000,0000,0000,,So it doesn't cause any printout, it just\Ncauses Python to remember. Dialogue: 0,0:13:48.80,0:13:53.94,Default,,0000,0000,0000,,I probably said that a few too many times.\NSo, so here is Dialogue: 0,0:13:53.94,0:13:58.97,Default,,0000,0000,0000,,a difficult problem, and I'll let you\Nthink about Dialogue: 0,0:13:58.97,0:14:00.16,Default,,0000,0000,0000,,it for a while. Dialogue: 0,0:14:00.16,0:14:03.11,Default,,0000,0000,0000,,I want you to kind of mentally go through\Nand execute this code. Dialogue: 0,0:14:05.33,0:14:10.60,Default,,0000,0000,0000,,And ask what, ask yourself what the output\Nof this program would produce. Dialogue: 0,0:14:11.84,0:14:12.83,Default,,0000,0000,0000,,How many lines? Dialogue: 0,0:14:12.83,0:14:16.22,Default,,0000,0000,0000,,How many lines of output would this\Nprogram produce? Dialogue: 0,0:14:22.19,0:14:26.93,Default,,0000,0000,0000,,So, how many of you said three? Dialogue: 0,0:14:28.29,0:14:34.04,Default,,0000,0000,0000,,How many of you said five?\NWell, the right answer Dialogue: 0,0:14:34.04,0:14:39.78,Default,,0000,0000,0000,,is actually three. You see five \Nprint statements, two, three, Dialogue: 0,0:14:39.78,0:14:46.37,Default,,0000,0000,0000,,four, five, but two of the print statements\Nare sitting inside of this. Dialogue: 0,0:14:46.37,0:14:51.27,Default,,0000,0000,0000,,And we never called, we never invoked a\Nfunction down here, okay? Dialogue: 0,0:14:51.27,0:14:52.42,Default,,0000,0000,0000,,So, this one, Dialogue: 0,0:14:54.36,0:14:58.99,Default,,0000,0000,0000,,let's clear this.\NThis one prints, these two get skipped, Dialogue: 0,0:14:58.99,0:15:00.58,Default,,0000,0000,0000,,this one prints, and this one prints. Dialogue: 0,0:15:00.58,0:15:03.60,Default,,0000,0000,0000,,So that that's why there are three\Nstatements that print. Dialogue: 0,0:15:04.92,0:15:09.96,Default,,0000,0000,0000,,There is stored, but we never used, a\Nfunction called print lyrics. Dialogue: 0,0:15:09.96,0:15:13.52,Default,,0000,0000,0000,,And it's got two statements in it, but we\Nnever used it. Dialogue: 0,0:15:13.52,0:15:19.68,Default,,0000,0000,0000,,So the output of this is \NHello Yo 7, and that's because we Dialogue: 0,0:15:19.68,0:15:21.08,Default,,0000,0000,0000,,never actually invoked it. Dialogue: 0,0:15:21.08,0:15:26.70,Default,,0000,0000,0000,,We had to say print lyrics parentheses\Nor whatever to cause it to call this. Dialogue: 0,0:15:26.70,0:15:29.26,Default,,0000,0000,0000,,Okay? That's just to emphasize that as it Dialogue: 0,0:15:29.26,0:15:32.49,Default,,0000,0000,0000,,looks at it, it does not execute \Nthese lines. Dialogue: 0,0:15:33.99,0:15:39.17,Default,,0000,0000,0000,,So once we've defined a function, once we\Nhave given it a name, given it Dialogue: 0,0:15:39.17,0:15:44.55,Default,,0000,0000,0000,,code that is a part of it, then we can invoke\Nit or call it as many times as we like. Dialogue: 0,0:15:44.55,0:15:45.36,Default,,0000,0000,0000,,So now, Dialogue: 0,0:15:45.36,0:15:48.80,Default,,0000,0000,0000,,our little example works a little better\Nif we actually call our function. Dialogue: 0,0:15:51.36,0:15:53.57,Default,,0000,0000,0000,,Python really doesn't care if you \Ndon't call your function. Dialogue: 0,0:15:53.57,0:15:55.85,Default,,0000,0000,0000,,It's like you told me to \Nmake one, I made one. Dialogue: 0,0:15:55.85,0:15:57.89,Default,,0000,0000,0000,,You didn't use it.\NThere you go. Dialogue: 0,0:15:57.89,0:16:02.86,Default,,0000,0000,0000,,But if you look at this one now, \Nso here we go, x equals 5. Dialogue: 0,0:16:02.86,0:16:05.53,Default,,0000,0000,0000,,print Hello, out comes Hello. Dialogue: 0,0:16:05.53,0:16:06.31,Default,,0000,0000,0000,,Define. Dialogue: 0,0:16:06.31,0:16:08.05,Default,,0000,0000,0000,,Nothing happens here.\NNothing happens here. Dialogue: 0,0:16:08.05,0:16:09.01,Default,,0000,0000,0000,,It's just remembering. Dialogue: 0,0:16:10.54,0:16:12.87,Default,,0000,0000,0000,,Okay? Then it says, print Yo. Dialogue: 0,0:16:12.87,0:16:16.43,Default,,0000,0000,0000,,Then it calls the function print lyrics,\Nwhich sort of stops us here, Dialogue: 0,0:16:16.43,0:16:20.70,Default,,0000,0000,0000,,runs these two lines of code.\NSo out comes that and that. Dialogue: 0,0:16:20.70,0:16:26.60,Default,,0000,0000,0000,,Then it sort of finishes this and it comes\Nback, x equals x plus 2, then it prints x. Dialogue: 0,0:16:26.60,0:16:28.98,Default,,0000,0000,0000,,That must mean that x is 7, \Nand so out that comes. Dialogue: 0,0:16:28.98,0:16:35.31,Default,,0000,0000,0000,,And so, again, it's on the \Nfirst time through. Dialogue: 0,0:16:35.31,0:16:36.56,Default,,0000,0000,0000,,No, go back, go back, go back. Dialogue: 0,0:16:37.83,0:16:40.50,Default,,0000,0000,0000,,On the first time through, it \Ndoesn't print. Dialogue: 0,0:16:40.50,0:16:41.55,Default,,0000,0000,0000,,But then when it hits this, Dialogue: 0,0:16:41.55,0:16:42.68,Default,,0000,0000,0000,,it prints. Dialogue: 0,0:16:42.68,0:16:44.53,Default,,0000,0000,0000,,You could say print lyrics \Nseveral more times and Dialogue: 0,0:16:44.53,0:16:46.70,Default,,0000,0000,0000,,it would run this as many times as it did, Dialogue: 0,0:16:46.70,0:16:50.63,Default,,0000,0000,0000,,and it needed to, as many times as you\Nwant, and it would make output for you. Dialogue: 0,0:16:50.63,0:16:55.15,Default,,0000,0000,0000,,So you can invoke this is the definition,\Nlet's clear this. Dialogue: 0,0:16:56.51,0:17:03.32,Default,,0000,0000,0000,,This is the definition.\NThis is the call or invoke. Dialogue: 0,0:17:04.02,0:17:06.56,Default,,0000,0000,0000,,So we are invoking the function, \Nwe're calling the function, Dialogue: 0,0:17:06.56,0:17:09.70,Default,,0000,0000,0000,,we're causing the function to execute. Dialogue: 0,0:17:09.70,0:17:14.11,Default,,0000,0000,0000,,Here we're just causing the function \Nto be looked at and defined, Dialogue: 0,0:17:14.11,0:17:17.41,Default,,0000,0000,0000,,but not actually executed. \NHope that's clear. Dialogue: 0,0:17:17.41,0:17:23.41,Default,,0000,0000,0000,,Now, when we pass data into a \Nfunction, and functions Dialogue: 0,0:17:23.41,0:17:26.32,Default,,0000,0000,0000,,that don't take data are not as\Nuseful as they could be. Dialogue: 0,0:17:26.32,0:17:28.24,Default,,0000,0000,0000,,There are plenty of things that do, times Dialogue: 0,0:17:28.24,0:17:29.71,Default,,0000,0000,0000,,that you build a function doesn't take\Ndata. Dialogue: 0,0:17:29.71,0:17:31.63,Default,,0000,0000,0000,,But the most interesting functions \Nare the ones Dialogue: 0,0:17:31.63,0:17:33.76,Default,,0000,0000,0000,,that you could hand them something to\Nwork on and Dialogue: 0,0:17:33.76,0:17:36.78,Default,,0000,0000,0000,,they could do their work and then come\Nback with whatever. Dialogue: 0,0:17:36.78,0:17:41.79,Default,,0000,0000,0000,,So this max function is a good example of\Nthis, one that's taking an argument. Dialogue: 0,0:17:41.79,0:17:44.08,Default,,0000,0000,0000,,We call the things in between the\Nparentheses Dialogue: 0,0:17:44.08,0:17:46.38,Default,,0000,0000,0000,,when we're invoking the function, Dialogue: 0,0:17:46.38,0:17:50.74,Default,,0000,0000,0000,,we call the things in between the\Nparentheses arguments, okay? Dialogue: 0,0:17:50.74,0:17:52.68,Default,,0000,0000,0000,,So that's passing into the function. Dialogue: 0,0:17:52.68,0:17:56.58,Default,,0000,0000,0000,,Feeding data into the function.\NSo we put arguments in between them. Dialogue: 0,0:17:58.90,0:18:03.00,Default,,0000,0000,0000,,So for example, here we have \Na little program. Dialogue: 0,0:18:03.00,0:18:04.50,Default,,0000,0000,0000,,That Dialogue: 0,0:18:05.98,0:18:09.27,Default,,0000,0000,0000,,that is, it's a function named greet, and\Nnow we are Dialogue: 0,0:18:09.27,0:18:12.28,Default,,0000,0000,0000,,going to define this function and we're\Ngoing to say, you know what? Dialogue: 0,0:18:12.28,0:18:15.82,Default,,0000,0000,0000,,I would like to take a parameter, let's\Ntake a parameter. Dialogue: 0,0:18:15.82,0:18:17.88,Default,,0000,0000,0000,,Let's have one parameter come in. Dialogue: 0,0:18:17.88,0:18:19.54,Default,,0000,0000,0000,,And we need kind of a placeholder for that Dialogue: 0,0:18:19.54,0:18:22.74,Default,,0000,0000,0000,,parameter, so within the function we're\Ngoing to use lang. Dialogue: 0,0:18:22.74,0:18:24.64,Default,,0000,0000,0000,,Now this isn't actually a real variable. Dialogue: 0,0:18:24.64,0:18:28.13,Default,,0000,0000,0000,,It's kind of like a, it's a placeholder\Nvariable. Dialogue: 0,0:18:28.13,0:18:31.07,Default,,0000,0000,0000,,So this first parameter, whatever it is, Dialogue: 0,0:18:31.07,0:18:32.97,Default,,0000,0000,0000,,when it's called, is going to be lang. Dialogue: 0,0:18:32.97,0:18:36.56,Default,,0000,0000,0000,,And so if that first parameter is \Nequal to es, Dialogue: 0,0:18:36.62,0:18:38.73,Default,,0000,0000,0000,,we're going to print Hola. Dialogue: 0,0:18:38.73,0:18:42.76,Default,,0000,0000,0000,,And else if it's equal to fr\Nwe'll print Bonjour. Dialogue: 0,0:18:42.76,0:18:44.48,Default,,0000,0000,0000,,And otherwise, we'll print Hello. Dialogue: 0,0:18:44.48,0:18:48.95,Default,,0000,0000,0000,,So there's apparently three languages in\Nthe world, Spanish, French, and English. Dialogue: 0,0:18:48.95,0:18:51.71,Default,,0000,0000,0000,,And if it's not Spanish or French, then it\Nmust be English. Dialogue: 0,0:18:51.71,0:18:56.34,Default,,0000,0000,0000,,But, I, you have to keep this kind of\Nsmall, so my screen doesn't get too big. Dialogue: 0,0:18:56.34,0:18:59.16,Default,,0000,0000,0000,,So this is again just the definition and\Nif you type this Dialogue: 0,0:18:59.16,0:19:02.16,Default,,0000,0000,0000,,into the interactive thing it gives you\Nthis dot dot dot prompt. Dialogue: 0,0:19:02.16,0:19:04.24,Default,,0000,0000,0000,,And so we now have this thing called greet Dialogue: 0,0:19:04.24,0:19:07.82,Default,,0000,0000,0000,,and now we've extended Python to add \Nour own function to Python. Dialogue: 0,0:19:07.82,0:19:13.03,Default,,0000,0000,0000,,And now we can say greet en and so \Nit runs this code Dialogue: 0,0:19:13.03,0:19:18.63,Default,,0000,0000,0000,,except that en is lang and so that comes,\Nand then it prints Hello. Dialogue: 0,0:19:18.63,0:19:21.35,Default,,0000,0000,0000,,So out comes Hello.\NNow later we can say, oh, Dialogue: 0,0:19:21.35,0:19:25.74,Default,,0000,0000,0000,,I would to do a greeting, but this time\NI'm going to pass es in as it. Dialogue: 0,0:19:25.74,0:19:32.30,Default,,0000,0000,0000,,So lang becomes, for this execution, es.\NAnd then, so it prints out Hola. Dialogue: 0,0:19:32.30,0:19:39.07,Default,,0000,0000,0000,,And then the next execution, lang is fr.\NSo it executes this three times but Dialogue: 0,0:19:39.07,0:19:44.48,Default,,0000,0000,0000,,lang is different each time because we've\Npassed in different parameters each time. Dialogue: 0,0:19:44.48,0:19:47.13,Default,,0000,0000,0000,,So that's how we can kind of write \Ngeneral-purpose code Dialogue: 0,0:19:47.13,0:19:52.61,Default,,0000,0000,0000,,inside the function and then reuse that\Ngeneral-purpose code in different ways. Dialogue: 0,0:19:52.61,0:19:55.26,Default,,0000,0000,0000,,Okay? It's a real powerful, powerful\Nmechanism Dialogue: 0,0:19:55.26,0:19:57.31,Default,,0000,0000,0000,,that makes functions far more useful. Dialogue: 0,0:20:00.04,0:20:05.17,Default,,0000,0000,0000,,Now, functions don't necessarily \Njust have to do stuff. Dialogue: 0,0:20:05.17,0:20:08.46,Default,,0000,0000,0000,,A real powerful mechanism in a function is\Nwhat we call a return value. Dialogue: 0,0:20:11.27,0:20:13.62,Default,,0000,0000,0000,,So a function can take its \Narguments, do some work. Dialogue: 0,0:20:13.62,0:20:14.92,Default,,0000,0000,0000,,We've seen that. Dialogue: 0,0:20:14.92,0:20:20.55,Default,,0000,0000,0000,,And then it can return a value. And the key\Nto the return value is, when we call the Dialogue: 0,0:20:20.55,0:20:22.95,Default,,0000,0000,0000,,function, like we were calling max, Dialogue: 0,0:20:22.95,0:20:26.56,Default,,0000,0000,0000,,it gives us back some value like the \Nlittle w, okay? Dialogue: 0,0:20:26.56,0:20:32.54,Default,,0000,0000,0000,,So here we're going to make a function\Ncalled greet that takes no parameters. Dialogue: 0,0:20:32.54,0:20:35.21,Default,,0000,0000,0000,,Doesn't take parameters, but it has \Nanother keyword. Dialogue: 0,0:20:35.21,0:20:38.20,Default,,0000,0000,0000,,It's another reserved word in Python Dialogue: 0,0:20:38.20,0:20:41.07,Default,,0000,0000,0000,,and whatever we put on this Dialogue: 0,0:20:41.07,0:20:47.25,Default,,0000,0000,0000,,return statement shows up as the\Nreplacement in this expression. Dialogue: 0,0:20:47.25,0:20:50.56,Default,,0000,0000,0000,,So, whatever greet is, it runs greet and Dialogue: 0,0:20:50.56,0:20:53.29,Default,,0000,0000,0000,,then the return is kind of a \Nresidual value. Dialogue: 0,0:20:53.29,0:20:55.89,Default,,0000,0000,0000,,So if we say print greet, comma, Glenn. Dialogue: 0,0:20:55.89,0:20:58.19,Default,,0000,0000,0000,,it says Hello Glenn, because \Nthe return value Dialogue: 0,0:20:58.19,0:21:01.04,Default,,0000,0000,0000,,for the greet function \Nis the string Hello. Dialogue: 0,0:21:01.04,0:21:03.16,Default,,0000,0000,0000,,And if we say greet Sally, Dialogue: 0,0:21:03.16,0:21:04.97,Default,,0000,0000,0000,,it says Hello Sally. Dialogue: 0,0:21:04.97,0:21:09.71,Default,,0000,0000,0000,,And so, and it's run the code twice and\Nthe return function, return value has Dialogue: 0,0:21:09.71,0:21:14.10,Default,,0000,0000,0000,,been put in here instead. And so the Hello\Ncame there and the Hello came there, Dialogue: 0,0:21:14.10,0:21:15.51,Default,,0000,0000,0000,,so we get the two lines. Dialogue: 0,0:21:15.51,0:21:18.66,Default,,0000,0000,0000,,So, return is a statement that both\Nterminates the Dialogue: 0,0:21:18.66,0:21:22.52,Default,,0000,0000,0000,,execution of the function and \Ndefines the value of Dialogue: 0,0:21:22.52,0:21:25.77,Default,,0000,0000,0000,,what will be replaced when \Nthe function call comes back, Dialogue: 0,0:21:25.77,0:21:28.15,Default,,0000,0000,0000,,in the line that the function was\Ncalled from. Dialogue: 0,0:21:29.98,0:21:35.18,Default,,0000,0000,0000,,So here is a little smarter version of\Nour greet function. Dialogue: 0,0:21:35.18,0:21:40.34,Default,,0000,0000,0000,,It's very similar, it's called greet\Nstill, takes lang as a parameter. Dialogue: 0,0:21:40.34,0:21:44.47,Default,,0000,0000,0000,,And if the language is es, then \Nit returns the string Hola. Dialogue: 0,0:21:44.47,0:21:46.76,Default,,0000,0000,0000,,If the language is French, \Nit returns Bonjour. Dialogue: 0,0:21:46.76,0:21:47.95,Default,,0000,0000,0000,,Otherwise, it returns Hello. Dialogue: 0,0:21:47.95,0:21:49.57,Default,,0000,0000,0000,,So we're not actually doing \Nthe print, if you Dialogue: 0,0:21:49.57,0:21:51.50,Default,,0000,0000,0000,,go back on the other slides, \Nwe were printing. Dialogue: 0,0:21:51.50,0:21:55.62,Default,,0000,0000,0000,,But now we're just returning a string.\NOkay? Dialogue: 0,0:21:55.62,0:22:00.88,Default,,0000,0000,0000,,And so now, I can call print greet, \Nand pass en in, Dialogue: 0,0:22:00.88,0:22:04.09,Default,,0000,0000,0000,,so then that runs the code once, \Nwith lang equal to en. Dialogue: 0,0:22:04.09,0:22:06.88,Default,,0000,0000,0000,,And I get back Hello, and then\Ncomma, Glenn. Dialogue: 0,0:22:06.88,0:22:09.47,Default,,0000,0000,0000,,Then I call it again and I pass es in.\NAnd then Dialogue: 0,0:22:09.47,0:22:14.92,Default,,0000,0000,0000,,that time it returns, the return value\Nhere becomes Hola, a string Hola. Dialogue: 0,0:22:14.92,0:22:19.87,Default,,0000,0000,0000,,Hola Sally. And then Michael, \NI'll pass in one more time. Dialogue: 0,0:22:19.87,0:22:20.82,Default,,0000,0000,0000,,Lang is now fr, Dialogue: 0,0:22:20.82,0:22:25.99,Default,,0000,0000,0000,,the string fr, and so it returns a\NBonjour and Dialogue: 0,0:22:25.99,0:22:31.62,Default,,0000,0000,0000,,so the, the residual that is here is\NBonjour, and so out comes Bonjour Michael. Dialogue: 0,0:22:31.62,0:22:33.80,Default,,0000,0000,0000,,So there is lot to this, right? Dialogue: 0,0:22:33.80,0:22:34.91,Default,,0000,0000,0000,,You're passing stuff in. Dialogue: 0,0:22:34.91,0:22:37.52,Default,,0000,0000,0000,,You have this kind of placeholder\Nvariable. Dialogue: 0,0:22:37.52,0:22:42.02,Default,,0000,0000,0000,,And you have this return that sort of\Nappears where it was called from. Dialogue: 0,0:22:42.02,0:22:45.67,Default,,0000,0000,0000,,This goes in, does its work, it comes back,\Nand there's sort of the Dialogue: 0,0:22:45.67,0:22:47.41,Default,,0000,0000,0000,,residual value that sits here. Dialogue: 0,0:22:47.41,0:22:50.94,Default,,0000,0000,0000,,You don't have to have a return in a\Nfunction, but if you want to Dialogue: 0,0:22:50.94,0:22:55.14,Default,,0000,0000,0000,,do something with the value, then you have\Nto have a return in the function. Dialogue: 0,0:22:55.14,0:22:58.32,Default,,0000,0000,0000,,We call the functions that produce values Dialogue: 0,0:22:58.32,0:23:01.58,Default,,0000,0000,0000,,fruitful, and the other ones \Nare called void. Dialogue: 0,0:23:01.58,0:23:04.50,Default,,0000,0000,0000,,[LAUGH] So, that's a good name for them. Dialogue: 0,0:23:05.63,0:23:07.80,Default,,0000,0000,0000,,So, to review sort of this, arguments, Dialogue: 0,0:23:07.80,0:23:10.63,Default,,0000,0000,0000,,parameters, and results, if we \Nlook at max, Dialogue: 0,0:23:10.63,0:23:15.96,Default,,0000,0000,0000,,the original thing, where it's looking for\Nthe largest, lexicographically Dialogue: 0,0:23:15.96,0:23:21.95,Default,,0000,0000,0000,,largest letter, it looks Hello world is\Nthe argument that's passed in. Dialogue: 0,0:23:21.95,0:23:25.42,Default,,0000,0000,0000,,We have this sort of formal parameter here\Ncalled inp which is not really a Dialogue: 0,0:23:25.42,0:23:28.43,Default,,0000,0000,0000,,variable, it just happens to refer to\Nwhatever is the Dialogue: 0,0:23:28.43,0:23:32.25,Default,,0000,0000,0000,,first argument in any particular call. Dialogue: 0,0:23:32.25,0:23:35.95,Default,,0000,0000,0000,,And then it does its little thing and\Nruns loops and does all these things and Dialogue: 0,0:23:35.95,0:23:41.18,Default,,0000,0000,0000,,at some point it returns w, so that the\Nthing that comes out when the function Dialogue: 0,0:23:41.18,0:23:47.02,Default,,0000,0000,0000,,quits that becomes the replacement value\Nhere is a lowercase w string. Dialogue: 0,0:23:47.02,0:23:49.59,Default,,0000,0000,0000,,And then that is the w that \Ngoes over in the big. Dialogue: 0,0:23:49.59,0:23:54.89,Default,,0000,0000,0000,,So the return is what defines what comes\Nback here. Dialogue: 0,0:23:54.89,0:23:58.85,Default,,0000,0000,0000,,Because you think of this as, it's looking\Nat this. It suspends for the moment. Dialogue: 0,0:23:58.85,0:24:00.30,Default,,0000,0000,0000,,It runs this code. Dialogue: 0,0:24:00.30,0:24:00.89,Default,,0000,0000,0000,,It's holding. Dialogue: 0,0:24:00.89,0:24:02.31,Default,,0000,0000,0000,,It's holding itself here. Dialogue: 0,0:24:02.31,0:24:05.08,Default,,0000,0000,0000,,It's running this code and then \Nit comes back to here. Dialogue: 0,0:24:06.22,0:24:07.19,Default,,0000,0000,0000,,Okay? Dialogue: 0,0:24:07.19,0:24:10.20,Default,,0000,0000,0000,,And the return value is what defines\Ncoming back. Dialogue: 0,0:24:11.26,0:24:17.51,Default,,0000,0000,0000,,So, of course you can have more than one\Nparameter and they are in order. Dialogue: 0,0:24:17.51,0:24:18.82,Default,,0000,0000,0000,,So here we have an a and a b. Dialogue: 0,0:24:19.86,0:24:22.39,Default,,0000,0000,0000,,These, the name of these things doesn't\Nreally matter. Dialogue: 0,0:24:22.39,0:24:25.89,Default,,0000,0000,0000,,They're just relevant inside of the\Nfunction definition. Dialogue: 0,0:24:25.89,0:24:28.17,Default,,0000,0000,0000,,So we are going to add \Ntwo numbers together Dialogue: 0,0:24:28.17,0:24:31.86,Default,,0000,0000,0000,,by taking a + b, and then \Nreturning the sum. Dialogue: 0,0:24:31.86,0:24:34.13,Default,,0000,0000,0000,,The added variable is just kind of local\Nto this function. Dialogue: 0,0:24:34.13,0:24:38.14,Default,,0000,0000,0000,,And now we can say, you know, \Naddtwo, 3, comma, 5. Dialogue: 0,0:24:38.14,0:24:40.31,Default,,0000,0000,0000,,And then this will come back as 8, \Nand then 8 will get assigned Dialogue: 0,0:24:40.31,0:24:42.99,Default,,0000,0000,0000,,into x, and so that will \Nprint out 8. Dialogue: 0,0:24:42.99,0:24:45.34,Default,,0000,0000,0000,,And so you can have as many of \Nthese as you want Dialogue: 0,0:24:45.34,0:24:49.28,Default,,0000,0000,0000,,and the order matters and there is a\None-to-one correspondence. Dialogue: 0,0:24:49.28,0:24:51.04,Default,,0000,0000,0000,,3 goes to a and 5 goes to b Dialogue: 0,0:24:51.04,0:24:52.38,Default,,0000,0000,0000,,when the thing is called. Dialogue: 0,0:24:52.38,0:24:56.41,Default,,0000,0000,0000,,And then the return value again \Ncomes back. Dialogue: 0,0:24:56.41,0:25:01.08,Default,,0000,0000,0000,,Okay?\NSo that's, sort of, arguments. Dialogue: 0,0:25:01.08,0:25:04.85,Default,,0000,0000,0000,,And like I said, not all functions \Nhave to return values. Dialogue: 0,0:25:04.85,0:25:07.98,Default,,0000,0000,0000,,We call them void functions when they \Ndon't return anything. Dialogue: 0,0:25:07.98,0:25:10.86,Default,,0000,0000,0000,,It's totally fine for that to be the case. Dialogue: 0,0:25:12.49,0:25:16.35,Default,,0000,0000,0000,,So at this point you might be thinking\Nto yourself, okay, great, well I still Dialogue: 0,0:25:16.35,0:25:18.32,Default,,0000,0000,0000,,don't quite get why to use functions. Dialogue: 0,0:25:18.32,0:25:22.89,Default,,0000,0000,0000,,And in reality, in the first 10, 11\Nchapters of this book, other than Dialogue: 0,0:25:22.89,0:25:26.47,Default,,0000,0000,0000,,using lots of functions, we're not\Nreally going to spend a lot Dialogue: 0,0:25:26.47,0:25:30.50,Default,,0000,0000,0000,,of time making functions because most of\Nour programs are going to kind of be Dialogue: 0,0:25:30.50,0:25:33.65,Default,,0000,0000,0000,,that long and we're not going to \Ndo a lot reuse in the program. Dialogue: 0,0:25:33.65,0:25:39.04,Default,,0000,0000,0000,,And there'll be a time when your \Nprograms become complex enough. Dialogue: 0,0:25:39.04,0:25:41.45,Default,,0000,0000,0000,,You'll be like, oh, thank heaven for\Nfunctions. Dialogue: 0,0:25:41.45,0:25:44.58,Default,,0000,0000,0000,,I think it's premature to say you \Nmust use functions, even though Dialogue: 0,0:25:44.58,0:25:47.86,Default,,0000,0000,0000,,there are some exercises that just say,\Nhey, do this with a function. Dialogue: 0,0:25:47.86,0:25:49.80,Default,,0000,0000,0000,,Just so you kind of get the \Nunderstanding of a function. Dialogue: 0,0:25:50.88,0:25:52.37,Default,,0000,0000,0000,,You will find soon enough, Dialogue: 0,0:25:53.55,0:25:55.79,Default,,0000,0000,0000,,as your programs grow, you'll go like, oh, Dialogue: 0,0:25:55.79,0:25:57.48,Default,,0000,0000,0000,,I keep doing this same thing \Nover and over again. Dialogue: 0,0:25:57.48,0:25:59.28,Default,,0000,0000,0000,,Let me pull it up into a function and pass Dialogue: 0,0:25:59.28,0:26:02.63,Default,,0000,0000,0000,,a parameter in, have a return value, and\Naway you go. Dialogue: 0,0:26:02.63,0:26:04.56,Default,,0000,0000,0000,,Or, you might find that you're moving from Dialogue: 0,0:26:04.56,0:26:06.48,Default,,0000,0000,0000,,one program to another and you have this\Ncommon thing Dialogue: 0,0:26:06.48,0:26:09.57,Default,,0000,0000,0000,,that you want to do, so you make yourself\Na library that you drag along. Dialogue: 0,0:26:09.57,0:26:12.19,Default,,0000,0000,0000,,And we will do lots of libraries. Dialogue: 0,0:26:12.19,0:26:14.91,Default,,0000,0000,0000,,The book in the second half does \Nlots and lots of library Dialogue: 0,0:26:14.91,0:26:18.88,Default,,0000,0000,0000,,stuff, doing things like parsing XML and\Nthis, that, and the other thing. Dialogue: 0,0:26:18.88,0:26:24.13,Default,,0000,0000,0000,,So, so don't feel like you need to use\Nfunctions on every Dialogue: 0,0:26:24.13,0:26:27.93,Default,,0000,0000,0000,,assignment, because they're a natural\Nthing when a program gets big enough. Dialogue: 0,0:26:27.93,0:26:30.92,Default,,0000,0000,0000,,So, so just kind of understand them on a Dialogue: 0,0:26:30.92,0:26:32.96,Default,,0000,0000,0000,,mechanical level, but Dialogue: 0,0:26:32.96,0:26:34.93,Default,,0000,0000,0000,,it'll come to you at the right time Dialogue: 0,0:26:34.93,0:26:38.00,Default,,0000,0000,0000,,when it's time to start building \Nyour own functions. Dialogue: 0,0:26:38.00,0:26:40.85,Default,,0000,0000,0000,,So in this class, we kind of, you know,\Ntalked about functions. Dialogue: 0,0:26:40.85,0:26:41.73,Default,,0000,0000,0000,,Just got you started. Dialogue: 0,0:26:41.73,0:26:45.18,Default,,0000,0000,0000,,Talked about parameters, talked about\Nbuilt-in functions, Dialogue: 0,0:26:45.18,0:26:50.29,Default,,0000,0000,0000,,talked about return values, the \Nstore and reuse pattern. Dialogue: 0,0:26:50.29,0:26:52.63,Default,,0000,0000,0000,,So, the problems at the end of the Dialogue: 0,0:26:52.63,0:26:57.10,Default,,0000,0000,0000,,chapter for this particular chapter \Nare relatively straightforward, Dialogue: 0,0:26:57.10,0:27:00.79,Default,,0000,0000,0000,,in that, like I said, we don't have\Na real strong Dialogue: 0,0:27:00.79,0:27:05.63,Default,,0000,0000,0000,,need to do functions yet in this class\Nbecause the programs aren't large enough. Dialogue: 0,0:27:05.63,0:27:08.02,Default,,0000,0000,0000,,But I just said okay, take, take one Dialogue: 0,0:27:08.02,0:27:12.06,Default,,0000,0000,0000,,of your previous assignments and \Nrefactor the code Dialogue: 0,0:27:12.06,0:27:16.88,Default,,0000,0000,0000,,so that at the top there is a def \Ncomputepay, and you put like the if Dialogue: 0,0:27:16.88,0:27:19.83,Default,,0000,0000,0000,,and whatever in here, and then later on Dialogue: 0,0:27:19.83,0:27:22.27,Default,,0000,0000,0000,,you do your code and then you call \Ncomputepay. Dialogue: 0,0:27:22.27,0:27:24.20,Default,,0000,0000,0000,,So you took code that you already had, you Dialogue: 0,0:27:24.20,0:27:27.14,Default,,0000,0000,0000,,move it up into a function, and \Nmake a function. Dialogue: 0,0:27:27.14,0:27:29.16,Default,,0000,0000,0000,,And I've also online got sort of a Dialogue: 0,0:27:29.16,0:27:31.81,Default,,0000,0000,0000,,sample of this, because it's, it's \Na little complex. Dialogue: 0,0:27:31.81,0:27:36.82,Default,,0000,0000,0000,,And so you should be able to find, on\NPython Learn or on the course site, Dialogue: 0,0:27:38.14,0:27:39.76,Default,,0000,0000,0000,,you should be able to find a good example, Dialogue: 0,0:27:39.76,0:27:41.67,Default,,0000,0000,0000,,because I really want you to \Nsort of get this. Dialogue: 0,0:27:43.32,0:27:45.29,Default,,0000,0000,0000,,Like I said, there will \Ncome a time Dialogue: 0,0:27:45.29,0:27:47.74,Default,,0000,0000,0000,,when functions will make the most \Nsense to you. Dialogue: 0,0:27:47.74,0:27:51.99,Default,,0000,0000,0000,,But coming up next, of course, is\NChapter Five, and that's loops. Dialogue: 0,0:27:51.99,0:27:55.11,Default,,0000,0000,0000,,And loops are going to rock the house,\NAnd so we really, that's our Dialogue: 0,0:27:55.11,0:28:00.53,Default,,0000,0000,0000,,fourth major pattern is loops and \Nand I'm looking forward to it. Dialogue: 0,0:28:00.53,0:28:02.62,Default,,0000,0000,0000,,So we'll we'll see you at the next lecture.