[MUSIC] >> Now that we understand conceptually what a function is, we have to figure out how to actually use them in Python. So, in this video we are going to focus on how do we actually call a function in Python and use its result? All right, so let's take a look. Now, in order to show you how to call a function, I have to actually have a function for you to call. Now, we're going to cover how to write a function in a subsequent lecture here. But for now, we're going to take this function that I already wrote, and it's called Useless, all right? Now, just like variables, your function name should be useful, right? They should explain what the function is doing, and I have followed that here. This function truly is useless, right? No matter what you do, this function is going to return three. And I can't think of much use for that, so I'm going with the word, the name useless, right? Functions also have parameters, okay? There's values that you can pass into the function. Those parameters have names. Here, here I've called them input1, input2, input3, all right? There are a bunch of inputs to the functions, so this is not unreasonable, but in general you should name them more usefully. If the function were useful, maybe it actually would need these inputs and then maybe they would be, I'd have some idea of what I should actually call them. One more thing I want to call your attention to. As someone who's going to call the function, I need to know the name of the function and I need to know how many parameters it has. Also, there's whats called a doc string here, and that describes what the function does, okay? And so that's useful to me as a person calling it, telling me, hey, if you call this function, here's what's going to happen. And this says this function takes three arguments and always returns three. Well, all right, great. Okay, it definitely tells me what it does, all right? So there you have it. I have a function that we can now call. So let's actually call the function. You can see here how I do that, right? I use the function name. And then an open parenthesis. And then I pass all the arguments to that function. In this case, this function requires three arguments, so I have three numbers, 1, 2, and 3. And I separate each argument by a comma. So I have 1 comma 2 comma 3 here. And then a close parenthesis, okay? So the function name. And then inside the parenthesis all the arguments separated by commas that you're going to pass, right? Let's run it. Nothing happened. Well, I guess you probably predicted that, right? I haven't printed anything, so nothing should happen, right? Everything worked out as expected. I called the function useless( 1, 2, 3), it returned the number 3 to me, but I didn't care. I didn't do anything with it. So let's actually do something with it, let's print the value, okay? A function call is an expression that evaluates to whatever the function returns. So we know what useless is going to do ahead of time, right? We know it's going to return the number 3, so I would expect when I print, call the useless with, now this time I pass 4, 5, and 6 since I know the inputs don't matter at all, right? I would expect to see a 3. A lo and behind, I do. Okay, so I can print the resulting value of a function just like any other expression, and it prints whatever that function returns. I can also save the values, right? So, variables can be used to save values. Functions produce values, so just like I can save the value of any arithmetic expression, I can save the value of a function call, right? So I call useless now with 7, 8, 9, right? Again, because I know it doesn't matter what I pass to it. And I assign the result of that function call to that variable result, right? That I printed out, so I would expect to see 3 again. And there we go, I do, okay? So I can call the function with whatever arguments I need to, but I have to make sure I pass in the arguments. And then it returns whatever it's going to return. And I have choices here, right? The first case, I just threw it away, I didn't do anything. And sometimes that valuable, if the function does something else, besides return, imagine the function was printing out some message, then maybe I don't care what it returns, I just wanted it to do it's job, which was to print out that message. I can just print the return value directly, right? I can evaluate it as an expression, I can actually use it in an expression directly. I can just say print useless 4, 5, 6 plus 7. In fact, let's do that, right? I can use it as a part of a larger expression. Okay, if I do that Hey, it returned 10. Works just fine. I can also save away the value that the function returns into a variable, which is what I did here, and that allows me to keep using it later. And so I can print result later, or I can use it in another expressions later. Now, one final thing i want to point out here is that you have to pass in the correct number of arguments. This function has three parameters, Input 1, Input 2, and Input 3. So, I have to pass it three things, okay? And every time I called it before, I did so. But let's look at what happens when you don't, okay? So if I call useless with just open and closed parenthesis, nothing in there, I'm passing zero arguments, right? I have nothing to pass, I'm not and the function might not need anything. And so that may be legitimate. It's not here. So, let's see what happens when we do this. Python say's, hey, there's some kind of error here. Let's read it. TypeError: useless() takes exactly 3 arguments (0 given). That seems pretty straight forward. I was supposed to pass three and I passed zero. So, it says, no, you can't do that. All right, fine. Let's Do this, let's try to pass just one number, all right? Let's see if we can guess what's going to happen here. I got basically the same message. Type error, useless takes exactly three arguments and only one was given. Okay, so it recognized that I tried a little bit harder, I gave one argument instead of zero, okay. And now, I'm certain, you can guess what's going to happen here when I pass two arguments, okay? Exactly the same error. It takes three arguments, exactly three arguments, two given. So you need to make sure that you pass the correct number of arguments to a function when you call it. Hopefully, you understand how to call a function now, all right? I need to know the function's name, I need to know how many arguments I'm supposed to pass it, and I kind of would like to know what it does too, all right? And I can get all that from the signature there, and it has it's name, the number of arguments, and it gives you names to the arguments, too, hopefully giving you some clue about what you're supposed to pass. And then there's that doc string that tells you what actually will happen if you call this function, all right? Then I call the function, I use the name, and then inside the parentheses I give it all the arguments that I want to pass. And I do that, the function does its magic, and it returns something back to me. And now I can take that returning, return value and I can do whatever I'd like with it. I can throw it away, I can store in a variable, I can print it out, I can use it in a larger expression, okay? So hopefully, now we have a handle on how to call a function, although we still kind of need to learn how to write one.