2.6 Write your own functions - Video Tutorials & Practice Problems
Video duration:
18m
Play a video:
Video transcript
<v ->So now let's talk about functions and methods</v> and we've already used some functions like print, input, length but how do we create our own functions and what exactly are they? So when we create our own functions, we can think of it as defining a recipe or defining a set of rules to do each time we call this function. So, like, when you have a bunch of steps that you have to do to make sushi then we can also do it in code and say, whenever I call this single function, I want to do this and this and this. Functions can also take different values in, different parameters. So, if I had a make sushi function, I could pass in a parameter of what kind of stuff do you want inside of it. Salmon, avocado, et cetera and those could all be passed as parameters to then be put inside of the sushi making function. So, let's look at how to actually write some of these functions. So, for doing our scratch work, I'm actually going to go into this Scratches and Consoles part of PyCharm. Right click, say New Scratch File, and then I'm gonna choose a Python file. So, this allows me to make a scratch Python file on the fly that's not gonna be used in the project and actually it'll be able to, unless you delete it, it'll be available for all projects in PyCharm that you use. So, I'm gonna show you here the basic way to define a function is to first use the keyword def, saying that you're defining a new function, and then you need to give the function a name. So for now, we'll just say, let's create an adding function. And it's gonna take two numbers, number one and number two or A, B, or whatnot. So, this is the name of the function, this can be whatever you want it to be called but it follows the same naming conventions as variables. So, it can't start with with a number. And then, you put these parentheses and anything that goes inside of the parentheses are now data that it's expecting when you call it and then you can use it inside the function. And they're separated by a comma. And then at the very end, you have this colon here and that says, after this line is done, treat all indented lines as if they belong to this upper definition. And then I'm writing this pass keyword just to say, like, do nothing in this function. So, I'm going to run scratch, nothing happens, I can call add with two numbers, nothing happens 'cause we're not doing anything in our function and not printing anything out. That's fine, I can see what it prints out. It prints out none because nothing is being returned yet and that's all fine. Eventually we want this to print out three. So, this is a good thing to do when you're creating functions is just kinda write this stub for it, like, what do I want it to be called? What is it taking in? And worry about the implementation afterwards. This is one kind of technique you can use. So now I'm going to have it actually return something, so we want it to do N1 plus N2 but when we run that this still says none. In order to say that the add function returns this value we have to use the return keyword. So, I'll run that again and now it returns three. And I can call this function with different values and I don't have to repeat this, I can just call this function, pass it different values and use it over and over and over again. So, this isn't a very useful function because it's pretty much just renaming an existing function but you can have lots of things happen inside of the function body before the return call. So, let's see, maybe I wanna print something out. (keyboard buttons clicking) You know, this is still not a very useful function but you can see that each time it gets called, it then evaluates everything that's inside of it's body, so it's all indented and does this one, then this one, then this one, then this one. Now actually, there will be some output, even if we don't print the result. (keyboard buttons clicking) And if we ever want to print what the result would be, just for debugging purposes, I can do that, and then result equals this, and return result, and print result here. So, the result here is three and this one is this big number. Okay, and then now I can delete all these other lines that I was using for debugging. So, the anatomy of a function here, let's look at this again, is I've got, I start off with DEF for define, it's a keyword, you can see 'cause it's in orange. Then we give it a name and this is defined by you, the user, and it's how you're gonna be calling the function in the future and then we got these parentheses to say that this is a function and anything inside the parentheses are what's being passed into the function so these two values and then a comma. That's everything. And then finally a colon at the end of the line and then everything inside the function is indented. So, it's a very visual language because it does rely on this indentation and this indentation, it'll-- PyCharm will do it for you automatically but you might accidentally, you know, delete it by accident and not indent the right amount. It should be four spaces and if they have differing amounts, like two different lines should belong to the same block but they have different amount of spaces before them, you'll see that there's also this syntax error here. We were gonna return it, we've got an indentation error, unexpended indent. So, it's not at the same level as this one. So it should be at four and then that's fixed it now. And then anything that is outside of this code block that's indented will then be at the same level here. So, once you define add then you can use it. So, I've got some questions. You know, and we haven't talked about going off and not just following along with what I'm typing but experimenting, seeing what happens. Like, if you have a question, test it out. So, this already brings up a few questions to me. Like, what if I try to use add before it's defined? Well, we have this little red underline here, so it probably won't work but let's actually just run it. Yeah, and that's right, name error, name add is not defined. So, just like the name error we got when we tried using variables before they were defined, add is not defined because it's a function that's defined later on. So, it'll read top to bottom. And then, let's see what other question did I have? Oh yeah, what happens if I don't pass in two values? What if I pass in none? Well, it says, type error, add is missing two required positional arguments N1 and N2. So, you do have to pass them in, in the right position. That's why these are called positional arguments and then also, if we pass in too many items it will also give us an error. So, to deal with these kinds of things, if you wanted to create a function that could take different amount of parameters you can do that but it's a little more, it's a little more advanced than we wanna get into right now. So, I'll talk about those in lesson five about making more complex functions. Okay, so now we're on to problem five and we're gonna get some stats about circles. So, what's the circles radius? And then we're going to provide the area and circumference of the circle given that radius. So, we'll go into problem five, circle stats and you can see I've got the stubs of some functions written here. Area given a radius R and circumference given a radius R and then we want to take some input and then also print out what the area and circumference are. Okay, so, let's just run this and let's put a circle radius of four. Nothing breaks, so that's good. And what we've got, nothing printed out here. So, what we want to do is get the area and circumference so we could put that straight into here where we're printing it out or we can save it as variables, as like, an intermediate step. Let's just do it here since it's a little cleaner. So, circle area equals area and then you pass in radius. So, you could call it, area equals area radius but area is already being defined as a function so that's a little, that seems a little buggy. Let's not do that. So, let's do this. It's saying that it doesn't return anything, that's fine. Let's just print circle area for now to see what we get. So, we're printing none. And then we wanna actually fill out this area function so the area of a circle is pi R squared and we saw in lesson 1.2 that we can get pi from the math library. We could define it up here, that would also be fine. We can say, pi equals 3.14159 but we can also get it from math. So, let's do that. So, import, math, and then so pi R squared is math.pi times R to the power of two. Okay and we'll run that. And we're getting a type error, unsupported operand type for the power functions string and int. So, we've narrowed it down to this power function, you can see the traceback. Now there's a couple of different lines on the traceback, this being the latest one, the one that error is actually in. So, this is a string and this an int, I guess. Let's just print that to make sure. And then it is in fact a type string. So, we could convert R to an integer or float here but that's like, you know, that's not really what area should be used for. You should be expecting a number. So, let's do it here. So, the circle radius, we can cast it to an int but what if, say, you put a float. So, like okay, maybe we'll cast it to a float. Let's see what happens. Okay, so it didn't break, that's good. It's still returning none and that's because area is not actually returning anything, it's just doing something, it's doing a calculation and then throwing it away and ignoring it. So, in order to have area return the value we have to put this return function in here. So, I will note that this is different from in Ruby where it will automatically return the last statement that was evaluated. So here, we put four and then now we get this number as a circle radius, that's great. Let's try just one, 'cause that should be pi times one squared which should just be 3.14159, great. And then try again with two, so that should be about 12 something, 12.56, great. So, I think this function is good, now we can just stop calling it here and put it here instead. And we have to cast it back into a string. Great. And then now let's just do the same thing for the circumference. So, I'm gonna return and then the circumference is pi times the diameter or pi times two, times the radius. So, we'll use math.pi times two, times R. Then we can call it circle diameter. Oh no, not diameter, circumference. Circumference and then radius. And then right here. (keyboard buttons clicking) Circle circumference. Okay, and they have the same one. I'll do this, so this should be pi times one, and this should be two times pie, and that's exactly what happens. So, this is our code, we've got these extra comments in here that we don't need anymore. And that's it.