 Welcome back everyone. Today we're going to talk about functions in Python. So looking back at what we've done before, we have the same setup. I'm just using a normal text editor like notepad and then I have my terminal up so I can run the commands from the command line. You might notice I already have a print hello in my new Python's file, Python function file. If I run python, story05, functions.py, then we just get hello printing out. Okay. So imagine I wanted to do something a little bit more complicated. Let's say that I wanted to print hello and someone's name. So I could say hello and then josh. Say print hello josh. Well now I have two print statements so I'm going to get two values printed if we run this again. So we have hello, which is the first line, and then hello comma josh is the second line. So now we're okay if josh is always the one running this script, we don't have any problem, right? But if multiple people are going to be running this script, you have a lot of different users, then the name probably is going to change. You don't want to say hello josh to somebody who's not called josh, right? Well, what we would do is you could assign a variable, let's call it name, and name here is john. So then we could do instead of hello josh, we could just do hello as a string comma space and then do our plus sign, which remember is concatenation and then the name variable. So then if we run that again, we get hello josh, which is this first line, and we get hello john, which is the name variable that we assigned to john. Then whenever a new user starts using the program, then they can just change their name. Let's say it's Kim. Then hello josh, hello Kim, right? So what you could do every time you run the script is ask the user for their name and then put the name in the variable and then just print hello name. Well, that's assigning a value to a variable and you have to know how you're going to get that name or whatever the value is. But let's say instead of, sorry, you might notice that I did something here, I'm using print twice. So I always have print hello josh. And then here I have my variable assignment and then I have print hello name. Well, I'm using the same code, almost exactly the same code twice. And in programming, we want to try to avoid typing the same code over and over again. So this is a very simple example of basically this piece of code and this piece of code are almost exactly the same. We want to try to reduce duplicating code as much as possible. And the way that we can reduce code is by using something called a function function. So so here this is just a comment called a function. Okay. And the way that we create a function, basically it's just a piece of code that we can call over and over again. So instead of just running this code, and then we run it one time and we never use it again until the next time the script runs, I can call the function as many times as I want. So to create a function, we start by saying def. And anytime you see def, that is the start of a function. And then we give it some name. So the function needs to have a name. So let's say, I'm going to give the function a name, say hello. And I'm writing this in camel case. So I have my first character lowercase and then any additional words are uppercase. So I have a function definition. And it is say hello. Okay, and then we need brackets. And then that's pretty much it. So it's very similar structure to an if statement. We've already talked about if statements in a prior video. So say hello is the name of the function that we're defining here. Then next we can put any of the code that we actually want to run. So in this case, I want to print hello. I'm going to move this print Josh down first. So you can see what's going on. So I'm going to add print hello Josh. Okay, so now we have our variable assignment with a name. We have print hello with the variable. And then I'll remove this. And then we have a function definition named say hello, that takes no arguments, has no parameters, and then print hello Josh, we have two print statements still. So let's run this and just see what happens. The only thing that's running is hello Kim, which is this first block of code at the top. And the reason for that is because I'm not calling I'm not asking this function to run. So what happens is we run the script, this top load of block of code will run, and then we will skip over the function until we want to run it. Okay, so we can then control when a particular function runs. Okay, so I'm just going to remove this. So let's say how do we actually run this? And first we define our function somewhere in our script. And then if we want to run the function, all we have to do is type, say, hello, and brackets. And that's it. So we've defined our function. If we call the function, it will print hello Josh. And then I can just call the function's name anywhere in my script. And this code, this block of code will run. So let's run that and see what happens. So hello, Josh. Now imagine I wanted to print this many, many times. Well, I could just copy and paste the print statement. And print statement isn't very big. So maybe that's okay. Or I can just call that function multiple times. Okay. And then hello, Josh prints multiple times. Now, let's say, of course, we don't want to try to duplicate this code either. So what could we do instead? How could we repeat this function call multiple times without repeating the same code over and over again? Hopefully you thought of something kind of like a while loop. So let's say x equals zero. Remember, and I think it was one of the first videos we had while x is less than five, do say hello, and then we can delete these others. And then we want to increment x by one. So we're assigning x to zero. While x is less than five, say hello, and then increment x, add one to x, right. So then what will happen is we will run say hello, four times, five times. Okay, yeah. So we started with zero. Okay. So it runs five times. And we only had to use the code, the same block of code one time, right? So we've removed duplication in our script, it'll make your scripts much smaller. Plus now anytime I want to print, hello, Josh, all I have to do is call this function. So functions, especially whenever you're first starting programming, it might seem like a lot of extra work. But whenever you start breaking your code down into functions, they become very reusable. And you want your code to be as reusable as possible while you're coding, it'll end up making your coding much faster and much more easy to manage. So here, instead of typing hello, Josh, many different times, I just wrote a small while function and then call the while loop and call the function over and over again. Now, if all you're doing is printing, you don't get a lot of advantage because this code is also as long as just printing three or four times. However, most functions are way more complicated than just a print hello, Josh. So you'll see that as we go along. Okay, so the main point here is we have a function, we define it with def. So anything starting with def is a function, a block of code that we can call, then it has a name and you can call this whatever you want. Instead of say hello, I could just say say hi. But whenever I'm calling it, then I need to say I need to actually give the functions name properly. Let's run it again. And then hello, Josh. Okay. Right, so there's a lot of other things we can do with functions. But one of the most important is to add parameters. So I have my say hi function. And in these brackets that you see, you might be wondering what they're for. Basically, whenever you see brackets, you're looking at a function. Okay, either you're calling a function or here we're defining the function. But these brackets contain parameters for the function. So we can send variables to this function. And then this function can do something with those variables. So let's say, remember, we don't want to use the name Josh every single time. So why don't we send a variable name to this function? Okay, so what I can do, we have our define function, it's called say hi. And we send it a parameter, or it has a parameter name. And then we print hello. And just like before, we will do hello plus name. Okay, so now somewhere else in my code, let's say I have a variable called f in, and this would be first name. Okay, so let's say the f in equals Lucy. So that's the first name of whatever this user is. Okay, then we can call the function say hi, and then give it the argument here, give it the argument in brackets, f in. Okay, so say hi, f in, this is a variable, f in equals Lucy. So then our function runs, so we have say hi, name is then equal to Lucy. So what we should get out is hello comma Lucy. Okay, so whenever we're calling it, this is a argument. From the functions perspective, this is a parameter. Hello, Lucy. Okay, so here we're sending variables to the function. And that's important because the function can have its own variables. So let's say we have a variable called f in inside the function. And the name of f in is john. Okay, so we're defining a variable called f in and it's assigned the value of Lucy. We have another variable called f in inside the function named john. Now, what we're doing is sending Lucy to the function. And then we'll run in here. So I'm going to print f in. Notice I'm printing f in inside the function. So what do you think will happen? Let's take a look and see what happens. Okay, so we have hello Lucy. So we can say here hello name. So name is equal to Lucy, because we ran the we called the function say hi with f in and f in is assigned to the name Lucy. Okay, so then f in was Lucy, it became the value name. And then we printed hello Lucy. Now, the next thing that printed was john. Now, why did this print? Well, f in equals john and print f in. This is what we call scope. So functions have their own variable scope. So whenever you name a variable something up above, the function basically has its own space where it will keep its own variables. So if you use the same variables inside the function, let's say they'll be in a different scope. Don't really worry about that too much for now, you'll understand it more as we go along, but just realize that if you were expecting name to equal john, that's not going to work because the function f in is already assigned in a different scope. Okay, so just something to think about. So let's remove this. So now we have our function. And before I was assigning a variable, just like just like the variable, you can call a function with a value directly. So let's go back to Kim. So say I have f in equals Lucy, say hi, Kim, do you think hello will be Lucy or will it be Kim? If you guessed that it is Kim, you would be correct because f in equals Lucy. Well, this doesn't have anything to do with the function. I'm calling say hi, and I'm sending Kim to the function. So then it will say hello, Kim. Okay, right. So then one more thing I want to talk about. I'll remove these functions here, these variables. Let's say I want to call a function. But I want the function to do something and return a value. So that's a really common thing to do is to have a function take some pieces of data and then return the result back to you. So we might call create a function def add one. So the name of my function is add one. And I'm giving it a variable x. Okay, whenever you see something like x or y, sometimes, but not always, it's probably going to be an integer or some sort of number of some type. But not always, you just need to know what they're sending it. Okay. And then what we're going to do with this is use the command return, return, which you can see it turned red. That's a special command in Python. And we are returning x plus one. Okay. So what do you think this does whenever I call this function, if I send it and a value a number an integer, then it will take whatever that value was add one, and then return that value return the result. Okay. So let's go ahead and call this. If I call this now nothing will happen because I haven't actually called either of the functions just to prove that I'm going to run this and nothing happens. Okay. So let's go ahead and call add one. So let's say y equals add one. And let's give it the value three. So I'm going to send an integer three to add one. So what we should get back is four. So y should equal four. Notice on the right hand side, I have the function call. And I'm just assigning the returned value directly to another variable. So then if we just do print y, what do you expect to happen? We're sending it three. We're returning three plus one. So y should equal four. Okay. Yep. And then y equals four. Now if we, you know, change this value, no matter what we change it to, it should always become something else. Let's send it a string and see what happens just so we can see some errors. So I'm going to send it add one. Okay. So I'm sending it the string. Okay. And it's going to try to add one to it. Let's see what happens whenever that runs. Yeah. Okay. So just as I expected, this is a failure. Right. It's saying trace back and the trace back is basically telling you where something went wrong. And you can see this line y equals add one. Okay. Has a problem. Okay. And it was trying to send okay to return x plus one. And the error type is cannot concatenate string and int objects. So here we sent a string. Okay. And one is an int. So since this was a string, it thought plus meant concatenate, not add. And because those are just two different types of variables, it throws an error because we can't just combine them like that. So I just wanted to show you what it looks like whenever a script fails. Normally look at the very bottom to see exactly what was going on. And then from this trace back, you can figure out approximately where something was going wrong. So look at line eight, which would be this line. And then look at line six, which would be this line. So those two lines have some type of problem. And that type of problem is cannot concatenate string and int objects. So don't just, if something fails, don't just skip over this, make sure you read what this type type error is. It'll give you a lot of really good information to debug your code. Okay. So let's add or change add one back to six, run the code again. And then we get seven. So everything's good because this is an integer. And this is a integer. Now, if we put quotes around six, we get the same problem because the quotes, I'm basically saying I want the string six, not the number six. Okay. So that's a little, that's pretty much it about functions and a little bit about debugging and errors. So to create a function, we first use def. And then we give it a name. And this name can be whatever you want, but make sure you call your functions something that you will remember later. They should have something to do with what they actually do in the code. You can send or functions can have parameters, you can send arguments to the function. And functions let you call the same block of code with variables over and over again. So you don't have to type everything continuously over and over again. Yeah, repeating everything. Okay. Make sure whenever you're using functions, if you want to return some value, like your function does math or concatenate strings or whatever, you might want to return it back to a variable. So add one will be called. It will do what it's supposed to do, whatever your block of code is, and then it will return a value and then start again from this line. And then it will go to the next line as it's executing all of the code. Okay. So that's pretty much it for functions. There's a lot more that we can do with functions and Python has some really interesting special features of functions, but we'll get into those as we get into more advanced things. So that's it for today. Thank you very much.