 Hello, everybody. Today we're going to be taking a look at functions in Python. A function is a block of code, which is only run when you call it. So right here, we're defining our function. And then this is our body of code that when we actually call it is going to be ran. So right here, we have our function call. And all we're doing is putting the function with the parentheses. That is basically us calling that function. And then we have our output throughout this video. I'm going to show you how to write a function as well as pass arguments to that function and then a few other things like arbitrary arguments. Keyword arguments and arbitrary keyword arguments. All of these things are really important to know when you are using functions. So let's get started by writing our very first function together. We're going to start off by saying D E F. That is the keyword for defining a function. Then we can actually name our function. And for this one, we're just going to do first underscore function. And then we do open parentheses and then we'll put a colon. We'll hit enter and it'll automatically indent for us. And this is where our body of code is going to go. Now, within our body of code, we can write just about anything. And in this video, I'm not going to get super advanced. We're just going to walk through the basics to make sure that you understand how to use functions. So for right now, all we're going to say is print. We'll do an open parentheses. We'll do an apostrophe and we'll say we did it. And now we're going to hit shift enter. And this is not going to do anything. At least you won't see any output from this. If we want to see the output or we actually want to run that function and some functions don't have outputs. But if we want to run that function, what we have to do is just copy this. I'm going to put it right down here. And now we're going to actually call our function. So let's go ahead and click shift enter. And now we've successfully called our first function. This function is about as simple as it could possibly be. But now let's take it up a notch and start looking at arguments. So let's go right down here. And we're going to say define number underscore squared. We'll do a parentheses and our colon as well. Now, really quickly, when you're naming your function, it's kind of like naming a variable. You can use something like X or Y. But I tend to like to be a little bit more descriptive. But now let's take a look at passing an argument into a function. The argument is going to be passed right here in the parentheses. So for us, I'm just going to call it a number. And then we're going to hit enter. And now we'll write our body of code. And all we're going to do for this is type print and open parentheses and we'll say number. And we'll do two stars. At least that's what I call it a star and a two. And what this is going to do is it's going to take the number that we pass into our function. It's going to put it right here in our body of code. And then for what we're doing, it's going to put it to the power of two. And so when the user or you run this and call this function, this number is something that you can specify. It's an argument that you can input that will then be run in this body of code. So let's copy this right here. And then we'll put it right down here into this next cell and we'll say five. And so this five is going to be passed through into this function and be called right here for this print statement. Let's run it and it should come out as I believe 25. That is my fault. I forgot to actually run this block of code. So I'm going to hit shift enter. So now we've defined our function up here and now we can actually call it. So now we'll hit shift enter and we got our output of 25. Now, in this function, we only called one argument, but you can basically call as many arguments as you want. You just have to separate them by commas. So let's copy this and we'll put it right down here. Now we'll say number squared underscore custom and then we'll do number and then we'll do power. So now we can specify our number as well as the power that we want to raise it to. So instead of having two, which is what you call hard coded, we can now customize that and we'll have power. And now when we call this function, we can specify the number and the power. And both of those will go into this body of code and be run. And we can customize those numbers. So let's copy this and we'll say five to the power of three. And let's make sure I ran this. So let's do shift enter. And now we will call our function and let's hit shift enter. And we got five to the power of three, which is one hundred and twenty five. And just one last thing to mention is if you have two arguments within your function and you are calling it right here, you have to pass in two arguments. You can't just have one. So if we have a five right here, it's going to error out. We have to specify both arguments for it to work. Now let's take a look at arbitrary arguments. Now, arbitrary arguments are really interesting because if you don't know how many arguments you want to pass through, if you don't know if it's a one or two or three, you can specify that later when you're calling the argument. So you don't have to do it up front and know that information ahead of time. So let's define our function. So we're going to say define and then we're going to say number underscore args and we'll do an open parentheses and a colon. Now, within our argument right here, typically we would just specify here's what our argument will be. It will be number or it will be a word, right? But what we're going to do is something called an arbitrary argument. So it's unknown. So we're going to put star and then we'll say args. Now, you will see something exactly like this. Typically, if you're looking at tutorials, they'll have star args in there. Or if you're looking at just a generic piece of code, this is what it will look like. But for us, we're going to actually put number. So again, we have the star and then we have our arbitrary argument right here. And then we'll hit enter and we're going to say print open parentheses. And this is where it's going to get a little bit different. So we're going to say number and then we're going to do an open bracket and let's say zero and then we'll do that times. And then we'll say number again with a bracket of one. So in a little bit, once we run this and then we call this number args function right here, we're going to need to specify the number zero and the number one that's going to be called. So let's go ahead and run this and then we are going to call it. And let's say five comma six comma one two eight. So right up here, we did not know how many arguments we were going to pass through. It could be five. It could be a thousand. And we could also call in a tuple. And that's what this is right here. We're calling in a tuple. So what it's going to do now is when it calls this number, it's going to call the very first within that tuple, which will be that five. And then it'll also call in this number, which will be the first position, which is the six. So let's hit shift enter and it's going to multiply these numbers together. So five times six is equal to 30. Now, like I just said, this is a tuple. So we don't actually have to write out these numbers like we just did. We can pass through a tuple when we are actually calling this function. Let's do that right up here. Let's just create, let's call it args underscore tuple. And we'll do open parentheses and we'll do the same numbers. Let's just copy X, make it easier. And now we've created this tuple right here, which we can then pass in. And this is a lot more handy, a lot more specific. And this is most likely how someone would do something like this. But let's now create this. And now we can copy args tuple and pass it through. Now, really quickly, this is going to fail and I'm doing that on purpose, but I want to show you what you need to do in order to pass through this tuple. So right now it's going to say tuple index is out of range. All you have to do in order to use this is you have to specify a star before it, just like you did when you were creating your argument up here. You have to put a star in front of our tuple that we just passed through. And now let's try running this. And now it works properly. Now, the last two things that we're going to look at are keyword arguments and arbitrary keyword arguments. There are more things that you can learn and do within functions. But again, I'm just trying to teach you the basics to make sure that you understand how they work. So let's go right up here. And a keyword argument is kind of similar to this right here. And let's actually copy this and put it right down here. Now a keyword argument is very similar in that you're going to specify your arguments right here. But what we did up here, let me bring this down, when we actually called the function, what we did was we just put in a five and a three. And when we did that, it automatically assigned number to five and power to three. And that's totally fine. And you can do that. But if you want a little bit more control, you can use a keyword argument. So right here, we could say our is equal to five and number is equal to three. So I just switched it around, right? Number was assigned to five and power was assigned to three. But I just switched it to show you how this might work. So let's run both of these. And now it's three to the power of five, which is 243. So that essentially is a keyword argument. Again, it just gives you a little bit more control. You don't have to put them in specific positions, like if you're just calling multiple arguments. Now let's come right down here. We're going to create basically another custom function. So for this one, we're going to write define number underscore org, and then we'll do an open parentheses, a colon and enter. And what this one is, is this one is a keyword argument or an arbitrary keyword argument. Now to specify an arbitrary argument, all we did was a star and then we input number. But if we're doing a keyword argument, we actually have to have two stars right here. So let's start taking a look. And again, if you're doing arbitrary, it means we don't really know how many keyword arguments we want to pass into our function. So we're just going to put star star number and then later within our body of code. And when we're calling it, we'll be able to specify it. And just like the arbitrary argument before the arbitrary keyword argument means we really just don't know how many keyword arguments we're going to need to pass into our function. So to demonstrate this, let's write print doing open parentheses and we'll say my oops, you do an apostrophe. My number is, and we'll do just like that little space. And we'll say plus, and this is kind of where it gets a little interesting or a little bit more tricky. So we're going to say is number. So this is us calling our number. And then we're going to do a bracket. And then I'm actually going to go to calling the function. It's a little bit backward or a little bit different than what you might think. But when we're calling it, what I'm going to do is I'm going to say integer is equal to let's just do some random number. Now, when we're calling that keyword within our body of code, what we're going to do is we're going to actually type out integer just like this. And this looks a little bit different. But what this allows us to do is we can put as many keyword arguments in here as we want later. And I'll show you in just a second. But for us, we're just creating this key and this value when we are calling it within the function. So now when we create this and we run this. Oh, whoops, I forgot. This has to be a string. So let's run this again. Now what we'll say my number is 2309. Then we're going to add, I'll say plus, and this isn't going to look great, but we'll say my other number. This will all be in the same line. That's okay. My other number. And then we'll say number. And we can specify again what we want in there. So now we can go down here to where we're calling it. We'll just put a comma and we'll say integer, oops. Integer two is equal to do a random number. And then we'll put integer two right here. And then we'll add plus right here so we don't error out. We'll create this, we'll run this. And as you can see, both numbers were passed through. Again, the syntax is terrible. But now you can see that you have this arbitrary keyword argument right here and all we have to do is put number, number, and we can pass through as many of these arbitrary keyword arguments as we want, as long as we just specify it within our function when we're calling it. So that's all we're going to look at in today's video on functions. There are of course other things that you can do within functions and it can get a little bit more advanced, but I wanted to show you the basics, the meat and potatoes of things that I definitely think you should know in order to get started using functions. I hope that you were able to understand functions better because of this video. If you did, be sure to like and subscribe below and I will see you in the next video.