1 00:00:00,582 --> 00:00:06,552 So far, we've just been typing code into Code Sculptor and we typed statements one 2 00:00:06,552 --> 00:00:11,794 after another and they get executed immediately when we run our program. 3 00:00:11,794 --> 00:00:16,599 This is fine for simple programs. But a very powerful construct in 4 00:00:16,599 --> 00:00:21,404 programming is the function. What a function is, is a piece of code 5 00:00:21,404 --> 00:00:24,535 that you define that you can execute later. 6 00:00:24,535 --> 00:00:30,286 And so you only execute the code inside a function when you call that function. 7 00:00:30,286 --> 00:00:33,417 And I can call the function more than once. 8 00:00:33,417 --> 00:00:36,061 So this. Becomes a very powerful programming 9 00:00:36,061 --> 00:00:38,982 construct. I define a block of code once and then I 10 00:00:38,982 --> 00:00:42,361 can use it many times. So in this video, we're going to talk 11 00:00:42,361 --> 00:00:47,000 about how to define a function and we're going to show some simple functions and 12 00:00:47,000 --> 00:00:52,025 some simple calls to those functions. Alright, let's write some functions. 13 00:00:52,025 --> 00:00:56,625 Now, before we actually type any code we have to think about what we want. 14 00:00:56,625 --> 00:01:01,225 Beginning programmers often just start typing before they think about it. 15 00:01:01,225 --> 00:01:06,329 And before I write any function I should decide what the function is going to do. 16 00:01:06,329 --> 00:01:09,795 And a good way of doing that is to put it in a comment. 17 00:01:09,795 --> 00:01:14,221 So I am going to write a function that computes area of a triangle. 18 00:01:14,221 --> 00:01:17,541 Okay? And I wanna see all your functions have 19 00:01:17,541 --> 00:01:22,115 comments like this. You should always put a clear comment that 20 00:01:22,115 --> 00:01:27,942 shows, this is what this function does. Now, I'm just gonna type the function, and 21 00:01:27,942 --> 00:01:30,820 then we're gonna talk about it. . 22 00:01:34,720 --> 00:01:39,760 And there we are. Here's my function for computing the area 23 00:01:39,760 --> 00:01:44,972 of a triangle, alright. So I wanna talk about, two parts of the 24 00:01:44,972 --> 00:01:49,329 function. The first which is line two is called the 25 00:01:49,329 --> 00:01:54,882 header of the function. The second which is the rest of it is the 26 00:01:54,882 --> 00:01:56,916 body. Okay so what is the header? 27 00:01:56,916 --> 00:02:01,380 The header tells you about the function, so it starts with the keyword Def. 28 00:02:01,380 --> 00:02:05,711 Def is short for define and you're telling Python that you want to define a new 29 00:02:05,711 --> 00:02:08,093 function. Then I have the word triangle area. 30 00:02:08,093 --> 00:02:12,262 That is the name of the function and you can choose any name that you'd like. 31 00:02:12,262 --> 00:02:16,106 All names that are valid, variable names are also valid functions names. 32 00:02:16,106 --> 00:02:20,437 So you should pick a function name that is indicative of what the function does. 33 00:02:20,437 --> 00:02:22,819 So I'm gonna compute the area of a triangle. 34 00:02:22,819 --> 00:02:25,310 I want to call my function triangle area. Then. 35 00:02:25,310 --> 00:02:29,920 We have a parenthesis list of perameters and in this case this function takes two 36 00:02:29,920 --> 00:02:32,766 perameters, the base and the height of the triangle. 37 00:02:32,766 --> 00:02:36,921 The functions can take zero or more perameters so, I can have zero, one, two, 38 00:02:36,921 --> 00:02:39,369 100. How many perameters you need to compute 39 00:02:39,369 --> 00:02:41,475 whatever does you are computing. Okay. 40 00:02:41,475 --> 00:02:46,200 And you should also name your perameters in a way that is obvious to someone who is 41 00:02:46,200 --> 00:02:50,469 using your working area program. Here base and height are obvious names for 42 00:02:50,469 --> 00:02:54,580 their elements of a triangle. Okay, then we have a special character, at 43 00:02:54,580 --> 00:02:59,260 the end of the header is a colon, a colon has huge significance in Python, this 44 00:02:59,260 --> 00:03:03,220 means you're about to start a new block of code, and you'll notice. 45 00:03:03,220 --> 00:03:05,953 The body of the function, which is the rest, and this is. 46 00:03:05,953 --> 00:03:08,538 Which is this new block, after the colon, is indented. 47 00:03:08,538 --> 00:03:12,565 It doesn't matter how much it's indented, but it does matter, it's indented by the 48 00:03:12,565 --> 00:03:14,453 same amount. And this has significance. 49 00:03:14,453 --> 00:03:17,436 When you stop the indentation, the block of code is done, 'kay? 50 00:03:17,436 --> 00:03:21,214 So, everything that is indented after that colon is the body of the function. 51 00:03:21,214 --> 00:03:24,146 And you can see, I have two statements that are in the body. 52 00:03:24,146 --> 00:03:26,383 The first one is actually computing the Area. 53 00:03:26,383 --> 00:03:30,360 And you can go to Wikipedia if you've forgotten the Area for the, or the formula 54 00:03:30,360 --> 00:03:33,492 for the Area of a triangle. It's just one-half times base times 55 00:03:33,492 --> 00:03:34,143 height. Okay. 56 00:03:34,143 --> 00:03:38,568 So, I'm computing that to line three. Then at line four, I'm returning that 57 00:03:38,568 --> 00:03:40,690 area. Okay, so what does return mean? 58 00:03:40,690 --> 00:03:45,722 Return is another key word in Python that says, this is the output of that function. 59 00:03:45,722 --> 00:03:50,572 And so, you can output one thing from a function, in this case I'm outputting the 60 00:03:50,572 --> 00:03:54,089 value of the local variable inside the function area, okay? 61 00:03:54,089 --> 00:03:57,060 So, let's run this programme and see what happens. 62 00:03:57,420 --> 00:04:00,573 Hm. I keep hitting the run button here, 63 00:04:00,573 --> 00:04:04,224 nothing happens. Alright, this is signifigant. 64 00:04:04,224 --> 00:04:10,034 This defined a function, okay? No code executes when I run this program. 65 00:04:10,034 --> 00:04:13,934 Why? Because a function is a piece of code that 66 00:04:13,934 --> 00:04:19,660 I want to be able to execute later. It only executes when you call it. 67 00:04:19,660 --> 00:04:25,801 So let's actually call this function. Let's say, you know, A1 = triangle area. 68 00:04:25,801 --> 00:04:29,868 Let's have a base of three and a height of eight. 69 00:04:29,868 --> 00:04:33,215 Now. Beginning programmers often will just run 70 00:04:33,215 --> 00:04:36,720 this and say hey, great it works cuz you get any old output. 71 00:04:36,720 --> 00:04:40,819 Let's think about it first. What is the actual output that's expected 72 00:04:40,819 --> 00:04:42,007 here. Let's print A1. 73 00:04:42,007 --> 00:04:46,819 I assume that a triangle with a base of three and a height of h and have an area 74 00:04:46,819 --> 00:04:49,373 of twelve. Let's see if this actually works. 75 00:04:49,373 --> 00:04:51,750 It does, okay. Now, I wanna point out here. 76 00:04:51,750 --> 00:04:55,805 I assign this to A1 and then I printed A1, what did A1 get? 77 00:04:55,805 --> 00:05:00,490 A1 got the output of the function triangle area, what is the output? 78 00:05:00,490 --> 00:05:06,223 The output is whatever the function chose to return after the return statement, so 79 00:05:06,223 --> 00:05:12,027 in this case it was it was a variable area which had been assigned to the one half 80 00:05:12,027 --> 00:05:16,642 base times height, okay. Let's try it with something else A2 equals 81 00:05:16,642 --> 00:05:19,072 triangle area fourteen and. 2.'Kay. 82 00:05:19,072 --> 00:05:22,870 Print A2. What do we expect the output to be here? 83 00:05:22,870 --> 00:05:27,520 I expect it to be fourteen, and lo and behold, it is fourteen. 84 00:05:27,860 --> 00:05:33,060 Now I want you to think of these functions as a box. 85 00:05:33,060 --> 00:05:38,160 And this box has a name. In this case, it's Triangle. 86 00:05:38,440 --> 00:05:45,285 Area and there's some number of inputs, in this case there are two inputs, and some 87 00:05:45,285 --> 00:05:50,544 of them are outputs zero one, this case there's one output okay? 88 00:05:50,544 --> 00:05:55,459 The first input. Gets named base, the second input gets 89 00:05:55,459 --> 00:06:01,385 named height, something happens here, we create an output, we call it area and then 90 00:06:01,385 --> 00:06:05,262 it goes out. Now the key point of this is these names 91 00:06:05,262 --> 00:06:10,822 base, height and area, they only exist inside this box, they only exist inside 92 00:06:10,822 --> 00:06:16,235 the function, you don't actually use them anywhere else, alright, and what's 93 00:06:16,235 --> 00:06:22,968 happening when I call the function, triangle, area I call the function by 94 00:06:22,968 --> 00:06:27,669 using the function name in the parenthesis and giving some values three, two. 95 00:06:27,669 --> 00:06:30,606 Right. What happens is three goes in here two 96 00:06:30,606 --> 00:06:34,375 goes in there. So base gets the value three, height gets 97 00:06:34,375 --> 00:06:38,281 the value two. I compute the value of the area okay which 98 00:06:38,281 --> 00:06:41,434 becomes three and out goes three on this side. 99 00:06:41,434 --> 00:06:46,711 So if I were to print this I'd get three, if I'm gonna assign it to A, A would 100 00:06:46,711 --> 00:06:50,686 become three okay. This is valuable for many reasons right. 101 00:06:50,686 --> 00:06:55,894 A function is now a black box, I don't care what's inside of it, I don't care 102 00:06:55,894 --> 00:07:01,240 what's going on in this squiggle here all I know is that there are two inputs. 103 00:07:01,240 --> 00:07:04,268 Base and height and one output and I can just use it. 104 00:07:04,268 --> 00:07:06,724 I read the comment and I know what it does. 105 00:07:06,724 --> 00:07:09,124 Okay. When I'm implementing the function, I 106 00:07:09,124 --> 00:07:13,466 don't have to care how you call it. All I know is these two things are going 107 00:07:13,466 --> 00:07:17,808 to come into the input and now I can implement it in here, you know, one half 108 00:07:17,808 --> 00:07:22,036 base times height, forgive me for abbreviating base and height for B and H, 109 00:07:22,036 --> 00:07:25,667 that computes area. This goes to area Right base goes to here, 110 00:07:25,667 --> 00:07:30,220 height goes to here, the output of this goes to area and I get my output. 111 00:07:30,220 --> 00:07:35,459 Alright, back to our code. Let's write some more functions. 112 00:07:35,459 --> 00:07:41,360 Let's write a function that converts. Fahrenheit. 113 00:07:41,360 --> 00:07:46,168 If I can spell two Celsius. Kay, so, let's define the function. 114 00:07:46,168 --> 00:07:51,537 We use Def and we give it a name that makes sense Fahrenheit two. 115 00:07:51,537 --> 00:07:57,307 We'll be clever with the number two Celsius and we take a variable name, 116 00:07:57,307 --> 00:08:03,484 we'll, we'll be Clear about that too that the variable is the, the value in 117 00:08:03,484 --> 00:08:10,314 Fahrenheit of the temperature to convert to this I'm going to say Celsius equals 118 00:08:10,314 --> 00:08:16,216 five-ninths times Fahrenheit minus 32 and we will then return celsuis. 119 00:08:16,216 --> 00:08:19,926 Okay. So now we have a function that takes a 120 00:08:19,926 --> 00:08:26,250 single input just the value in Fahrenheit and returns an output in celsuis. 121 00:08:26,250 --> 00:08:33,895 Next we should test it we shouldn't just trust that it works so let's say c equals 122 00:08:33,895 --> 00:08:41,376 fair in height to Celsius. Let's take some values that we know the 123 00:08:41,376 --> 00:08:43,920 answer, okay. See one. 124 00:08:47,280 --> 00:08:52,205 To copy paste 2,12. So I know freezing and boiling, and we 125 00:08:52,205 --> 00:08:56,266 better make this c2. Let's print this out, c1, c2. 126 00:08:56,266 --> 00:09:00,500 I picked these because it's freezing and boiling. 127 00:09:00,500 --> 00:09:05,166 So I'm expecting zero and 100 to come out. And they do. 128 00:09:05,166 --> 00:09:09,660 So I probably wrote this correctly, Okay? [inaudible]. 129 00:09:09,660 --> 00:09:14,172 Maybe I don't like Celsius okay, I'm a big fan of Kelvin. 130 00:09:14,172 --> 00:09:18,605 I really feel like you know, absolute zero is my friend. 131 00:09:18,605 --> 00:09:23,198 So let's convert Fahrenheit to Kelvin instead of Celsius. 132 00:09:23,198 --> 00:09:31,674 So again I'll give it a useful name. I'll name the parameter Correct, I'll 133 00:09:31,674 --> 00:09:36,135 spell it correctly okay. Now what do I do. 134 00:09:36,135 --> 00:09:38,540 Well. Hang on a second here. 135 00:09:38,540 --> 00:09:42,777 I have a function that converts from Fahrenheit to Celsius. 136 00:09:42,777 --> 00:09:47,374 I know that Kelvin is Celsius plus 275. Thirteen, or 273.15, sorry. 137 00:09:47,374 --> 00:09:53,192 But I, I already know how to convert to Celsius so it would seem silly to try and 138 00:09:53,192 --> 00:09:57,430 write that code again, so let's not. Let's call our function. 139 00:09:57,430 --> 00:10:03,176 Inside of functions I can write any code, or I can use any code that I would use 140 00:10:03,176 --> 00:10:06,480 anywhere else. So if I already have a function. 141 00:10:07,100 --> 00:10:13,742 I can use that too, all right? And I know that the Kelvin value. 142 00:10:13,742 --> 00:10:19,326 I should be consistent here and not use bad variable names. 143 00:10:19,326 --> 00:10:24,341 Kelvin value is simply the Celsius value plus 273.15. 144 00:10:24,341 --> 00:10:27,748 And now I can return Kelvin. Alright. 145 00:10:27,748 --> 00:10:33,676 Well, I should test this. K1 equals Fahrenheit. 146 00:10:33,676 --> 00:10:40,710 Two Kelvin. Again, let's do some of the ones that we 147 00:10:40,710 --> 00:10:45,776 know. 212 hit two for in to k one, k two, 148 00:10:45,776 --> 00:10:51,249 alright let's run this, and we get the numbers that numbers that I expect here as 149 00:10:51,249 --> 00:10:54,356 well. All right, I think this is very important 150 00:10:54,356 --> 00:10:58,275 for you to understand. Right inside of a function I called 151 00:10:58,275 --> 00:11:02,193 another function. Right so let's think of this in terms of 152 00:11:02,193 --> 00:11:07,395 the boxes again, and hopefully you'll forgive me but I am going to use single 153 00:11:07,395 --> 00:11:10,570 letters here because I can't write fast enough. 154 00:11:10,570 --> 00:11:15,300 So I had a function Fahrenheit to Celsius, right there's some box here. 155 00:11:15,300 --> 00:11:17,285 Okay? And it had one input. 156 00:11:17,285 --> 00:11:23,167 And that input was my Fahrenheit value. I did some calculations, which, we know 157 00:11:23,167 --> 00:11:27,291 what they are, right? They're five-ninths, you know, f-32. 158 00:11:27,291 --> 00:11:30,652 Okay? I get out Celsius value, and that comes 159 00:11:30,652 --> 00:11:33,249 out. Then I wrote another function. 160 00:11:33,249 --> 00:11:36,686 I wrote a function Fahrenheit to Kelvin. Okay? 161 00:11:36,686 --> 00:11:39,076 That's also a black box. Alright. 162 00:11:39,076 --> 00:11:41,581 Incomes, one input. Something happens. 163 00:11:41,581 --> 00:11:44,643 We compute Kelvin, and Kelvin goes out, right? 164 00:11:44,643 --> 00:11:47,775 Now, I wanna make a, an interesting point here. 165 00:11:47,775 --> 00:11:52,507 I don't care how you implement Fahrenheit to Kelvin if I'm using it. 166 00:11:52,507 --> 00:11:57,865 So when I call Fahrenheit to Kelvin. So somewhere, I call Fahrenheit to Kelvin 167 00:11:57,865 --> 00:12:00,440 with 32. I just want the right answer. 168 00:12:00,440 --> 00:12:02,177 Right? I want the answer. 169 00:12:02,177 --> 00:12:05,000 I want this to evaluate to 273.15. Okay. 170 00:12:05,000 --> 00:12:07,533 How you do it? That's your business. 171 00:12:07,533 --> 00:12:13,468 So when you're in here implementing it, there's absolutely no reason why you can't 172 00:12:13,468 --> 00:12:16,798 say, hey, Celsius. I already know how to do that. 173 00:12:16,798 --> 00:12:20,417 F to c of f, right? But, you could also recompute it. 174 00:12:20,417 --> 00:12:23,674 You could say, you know, I like being messy. 175 00:12:23,674 --> 00:12:29,320 And I'll just say, instead of that, I'll say Celsius = five-ninths Fahrenheit -32. 176 00:12:29,320 --> 00:12:31,911 Okay and then I go to Calvin from there, alright? 177 00:12:31,911 --> 00:12:35,097 So there are many ways to right a function, which is better? 178 00:12:35,097 --> 00:12:39,470 I would argue that if I already have a function to do the job, I should not write 179 00:12:39,470 --> 00:12:41,900 the code again, you do not want repeated code. 180 00:12:41,900 --> 00:12:46,003 If I figure out "Oh I'm screwing this up, it's really nine fifths" or whatever 181 00:12:46,003 --> 00:12:48,433 right? You change it in one place and then it 182 00:12:48,433 --> 00:12:51,943 would be fixed in both, okay? I think that this is pretty critical 183 00:12:51,943 --> 00:12:54,426 alright? But keep in mind I have these boxes I 184 00:12:54,426 --> 00:12:58,962 don't need to care how they're implemented as I use them, and once I have a function 185 00:12:58,962 --> 00:13:04,334 it's available to me to use anywhere. Alright, I wanna show you one final 186 00:13:04,334 --> 00:13:09,140 function here. I wanna show you a function that prints 187 00:13:09,140 --> 00:13:13,946 hello world, 'kay. This function, I'm gonna call it hello, 188 00:13:13,946 --> 00:13:19,730 takes no arguments, alright. Remember, I said that we don't have to 189 00:13:19,730 --> 00:13:26,316 have any parameters or arguments, 'kay. And all it does is print hello world, 190 00:13:26,316 --> 00:13:27,562 'kay. That's it. 191 00:13:27,562 --> 00:13:32,906 Alright, let's test it. Cuz we should always test our functions, 192 00:13:32,906 --> 00:13:36,363 let's call hello. Alright, oops, I've got to spell it right 193 00:13:36,363 --> 00:13:41,094 or that definitely is not going to work, okay so what happens when we run this 194 00:13:41,094 --> 00:13:43,089 function? It prints hello world. 195 00:13:43,089 --> 00:13:44,276 Okay. That's great. 196 00:13:44,276 --> 00:13:49,086 That's what I expected it to do. Now, what's different about this function 197 00:13:49,086 --> 00:13:52,316 from all the other functions we've written today? 198 00:13:52,316 --> 00:13:57,720 Well, it does not have a return statement. This function, if I drew it with, as a box 199 00:13:57,720 --> 00:14:01,740 would have no inputs and no outputs. Nothing comes into hello. 200 00:14:01,740 --> 00:14:05,101 There are no parameters. Nothing comes out of hello. 201 00:14:05,101 --> 00:14:09,450 There's no return value. So what happens when you try to assign it? 202 00:14:09,450 --> 00:14:11,230 Say H equals hello. Print H. 203 00:14:11,230 --> 00:14:13,935 Right. Well there's nothing coming back, so what 204 00:14:13,935 --> 00:14:18,391 do you think's going to happen? Well, Python has a value none, and none 205 00:14:18,391 --> 00:14:23,453 means there's nothing there, okay? And so Python, if you forget the return 206 00:14:23,453 --> 00:14:28,796 statement, or if you don't need a return statement, it automatically adds one. 207 00:14:28,796 --> 00:14:31,889 So it's exactly like if I typed return none. 208 00:14:31,889 --> 00:14:36,037 Okay, this none is a special value. Alright, so H gets the value none. 209 00:14:36,037 --> 00:14:39,234 I can print H and it'll say none. Now why do I bring this up? 210 00:14:39,234 --> 00:14:43,176 Well, usually you're not writing functions like this, you might write a few. 211 00:14:43,176 --> 00:14:47,437 But often, you return a value and you expect it to return a value but you forgot 212 00:14:47,437 --> 00:14:51,859 the return statement and then it sh, none will show up here in the console for your 213 00:14:51,859 --> 00:14:56,014 variable and I just wanted you to see this is probably the most likely reason. 214 00:14:56,014 --> 00:15:00,010 You should go look at your function again and see if you forgot the return. 215 00:15:00,010 --> 00:15:02,561 Okay. So this was a quick and dirty introduction 216 00:15:02,561 --> 00:15:05,218 to functions. Functions are extremely powerful and 217 00:15:05,218 --> 00:15:08,833 extremely useful and every program you write will include functions. 218 00:15:08,833 --> 00:15:13,245 I've only scratched the surface here on what you can do with functions and we will 219 00:15:13,245 --> 00:15:17,178 see them throughout the course. Pretty much every lecture from now on will 220 00:15:17,178 --> 00:15:20,473 use functions, every project that you write will use functions. 221 00:15:20,473 --> 00:15:23,875 So you're gonna get familiar with them pretty quickly, all right? 222 00:15:23,875 --> 00:15:26,958 I want you to keep in mind the things that I've said here. 223 00:15:26,958 --> 00:15:31,104 And hopefully, the model of using a box, with the inputs and outputs inside the 224 00:15:31,104 --> 00:15:32,380 box. Those names stay in. 225 00:15:32,380 --> 00:15:36,380 Inside the box will be valuable to you. Don't forget the colon. 226 00:15:36,380 --> 00:15:39,669 Don't forget the indentation. And don't forget the return. 227 00:15:39,669 --> 00:15:42,340 These are critical pieces of the functions. 228 00:15:42,340 --> 00:15:47,115 Hopefully, with a little practice. You'll understand and be able to use them