 Hey everybody, this is Brian. Welcome to the 7th Python tutorial. Alright, we're going to dive right in here, maybe my mouse will actually work with me again. We're going to make the video 7, and we're going to call this one fun with functions. Gosh, I cannot spell. Alright, so what is a function? Whether you know it or not, you've probably worked with them before. We're going to use the word DEF, which is short for definition, and we're going to say do something. Now, inside of this definition, we're just going to say print hello world, and let's run this. As you can see, nothing happens. The reason for that is simple. A function or a definition in Python needs to be called, meaning this has its own unique scope. So if we actually, for illustrative purposes here, say start a program, we run this little guy, you'll see start a program executes right here, but this definition does not. Because we have to call this, and to call that, you literally just do this. Notice how you have to include these brackets here. There's our hello world. Now, if we take these brackets out, what happens? Well, it doesn't do it, because we're not actually calling it. At this point, we're just declaring a empty variable using that name. So you have to use those brackets. Now, let's make a little bit more of a complex function. Is that even English a little bit more of a complex function? It's been a long day, and I just got out of the gym, so bear with me here. We're going to say get list, and we're going to say max, and let's do this. x equal, let's see, I almost typed the word no, you can tell I'm tired, because I was thinking in C++ for a minute. All right, so we're going to make a new list with a range in there. I in x, and we're going to say x i equals i times, let's just pick a number here. Let's say 5, and then we're going to return x. So what we're doing is we're actually returning a value, meaning this function, this definition here, is going to run, and it's going to return x, meaning it's going to generate x, and then it's going to return it. I keep saying return, but what does that really mean? So what we can say here is, oh, say my list equal get list, and we're going to say 20, and then we're just going to print my list. Notice how we're assigning the value. What we're really assigning is x from here. We're calling get list, and we're assigning the value. Let's run this. Uh-oh, what have we done wrong? Oh yes, we forgot our little semicolon there. There we go, beautiful. So there is our list right here. Now, quick discussion on scope. Remember how we said in previous tutorials where you had to declare a variable before you can use it? Well, the same thing applies with definitions and functions. For example, if we take this my list, even though it's in the same scope, and we try to do this first before the definition's been declared, we get an error. See, get list is not defined. It treats it like a variable. Pretty neat, huh? Now, this is an example of a single parameter. That's what this little guy here is called a parameter. We're going to do a multiple parameter, and we're going to say def, get animal. Let's not say get animal. Let's say get person. I'll say name, age, equal zero. And we're just going to print. That was not good. You can tell I'm tired. And we're just going to say print. Now, there's a couple of things going on here in get person. First, we've got multiple parameters separated by a comma. You can have as many as you want. And this can be pretty much any data type. We could do a list, a dictionary, an integer, a string, whatever you want to do. And we're saying the person is, you know, whatever you want. And they are however many years old. Now, you also notice age equals zero, meaning we're actually setting a default value. That does a few things here. Whoops. If we say, we shouldn't call that get person. We should call it print person because we're not actually returning anything. So we're going to print person. And we're going to say, Brian, and I am 40 years old. Go ahead and run this. The person is named Brian. They are 40 years old. Now, if we omit the age, what's going to happen? The person is named Brian. They are zero years old. Notice how it used zero because that is our default value for that parameter. We could also do the same thing as name. Notice how if you supply the parameter, it will use your supplied parameter. If you omit it, it will use the default. Now, what happens if you don't use the default and you still omit it? If you said runtime error, you are absolutely correct. So why would you use a definition or a function as it's called? The reason why you would do this is you have chunks of code you want to run. For example, let's make it basic if then statement. So we'll say, we'll say h equal 8. Why not? If h is greater than 4, then we're going to print person. Also, we're going to print person. Let's just supply some values here. Yeah, she's 212 years old. That's how tired I am. 40. So what we're doing here is we're saying we have a variable h, which is 8. If h is greater than 4, we're going to print person Brian. Otherwise, we're going to print person Heather. So that's another example of polymorphism where you can call this chunk of code based off this value right here. To run that, and sure enough, it runs Brian. Set that to 2 just so we can see it change. There we go. Some little known issues. You have to declare it before you can use it. You should only add parameters that are necessary. Don't go crazy and have a function with 200 parameters. No one's going to use it. Some other things. Make sure the name is descriptive. What does it do? Does it get a list? Does it do something? Does it print a person? Don't name it my super awesome function that does I don't know what. Because no one's going to use it. That's all for this tutorial. Hope you found this educational and entertaining. Be sure to visit my website for the source code for this and other tutorials. If you're interested in other languages, I have a whole bunch of other tutorials up there as well.