Now, in order to actually use functions, we need to be able to write functions. So in this video, we're going to talk about, how do you actually write a function? What is the structure of a function? What does it look like and how do you define it? Right? So, let's get started. Let's start by looking at the elements of a function definition. All right? The first thing that you have to do when you want to write a function is, you start it out with the word def, D-E-F. This is short for define and you're telling Python, "I am going to define a function here." So def says, "Hey, this is a function. " Then I give the function name. In this case, the name is "sayhello". And as I've said, make your names useful. Your name should clearly describe what the function is doing. So, this function is going to say hello, I already basically know that just from the name itself. Then I have in parentheses all the parameters of the function. This function is not going to take any inputs, so I just have an open and closed parentheses. And then finally, I end the line with a colon. All right? And this says, "the definition is about to start." Okay. So, def to indicate that it's a function, function name, a parenthesized set of arguments, and then a colon. All right. Now, immediately on the next line I have to indent, and this is important in Python. You have to indent afterwards. Okay? It's not enough just to, you know, put the def name and colon, you have to then indent everything afterwards. And that tells Python the indented stuff is the body of the function. All right? And when you stop indenting, the function is done. All right? So we indent, and the first thing we have here is a string. And it's a multi-lined string, so it starts with three quotation marks and ends with three quotation marks. And, this is called a docstring. And you need to write in your docstring something that clearly describes what this function is going to do. So, this function is going to print, "Hello". All right? So, I now have two pieces of redundant information. I know that the function name is sayhello, indicating to me, "Hey, this thing is going to say hello to me, " and the docstring that says, "Hey," what it actually does is print, "Hello." All right? And then, I have the actual code, that is the body of the function which is print "hello", then unsurprisingly. Now, I can put any code here in the body of this function that I can put anywhere else in a Python program. They're not limited to just having one line here, you're not limited to having a print statement. You can have any expressions you want. You just need to, you know, keep them indented to keep them inside the function. Okay? Now you look after that, I stop indenting, that means the function is over. So now I've defined this function called "sayhello". I've told you what it does in the docstring, and I've written the code that actually does it. Right? It's just a single line, it's a very simple function. All right? Our function is going to be much more complicated than this as we go on through the course and subsequent courses. But, to start with, let's start with something easy. Now, we already know how to call a function. All right? Here, I just use the function name, "sayhello" and then open and close parentheses with all the arguments I want to pass. Well, I don't want to pass anything because this function has no parameters. So I don't need to, and in fact, I can't pass anything, and let's see what happens. Hey, the word "hello" shows up in the output. That's not surprising, that's what I expected. Okay? Well, you might be thinking at this point, "Why on earth did I bother doing this?" Okay? And we've talked about this a little bit, but let's talk about it again, here. Okay. I could have just written, "print hello". But, I now have this as a function and I can, you know, call it many times and it will do it over and over again. If later on in my program, you know, I've written this, you know, code to say hello 57,000 times, I decide, "Oh, well I don't want to actually say hello, I wanna to say bonjour." And I change it once and it fixes my problem everywhere, instead of having to hunt through the code everywhere that I printed "hello". All right? Now, sayhello didn't have any parameters, and it didn't return anything. It's a very simple behavior, all I did was print something out to the console. Okay? In general, I can have more than zero parameters. So let's look at this function called "double" here. All right? Again, I'm picking a name that hopefully indicates what's going to happen, and if I read the docstring, yeah, it kind of seems like it does. It returns twice the input values. So, double. I'm going to double whatever you give me. All right? And it does take an input, all right? And I'm going to call this parameter, "value". So I take value as an input, and I return twice this value, okay? And so, you notice that a function can return something. Inside of the body of the function, if I have a statement that says "Return", that means return this back to whoever called me. If I don't have this return statement here, you're saying, "I have nothing to give you back." Let's see what we can do now. Since we know returning something that this function is going to give me something, I can call double six and assign the return value to a variable called "result" and then I can print it. So, I hope, that this is going to return the number 12 from the function double, assign that to results. And the way I put results, I should see 12. And yes I do. Okay. So, "return" basically says, "This is what should go back to the color of this function," right? Remember, I can use function calls inside of expressions. When I call a function it evaluates to whatever its return value is. And I can use this, I can say, "Hey, let's actually assign result to be 32 plus double of six. So double of six, we evaluate their function by calling it, it's going to return something. Whatever it returns, I'm going to add the 32. Okay. And here we go, we get 44. All right? So, return is important piece of a function, as most functions are going to return some value to you because you're going to be asking the function to compute something for you. So it's going to do whatever computations it's going to do, and then return to you the results. Now, again, this is a one-line function. There is nothing restricting functions to be a single line. I just wanted to show you that you can return something and it doesn't have to be complicated. Note that, what is on the return line is also an expression. I can put anything there. I do not need to have an arithmetic expression, I could just say return value. All right? Now we're not doubling things anymore, let's get rid of the 32 so that we can more clearly see this. Right? I can return value, I just get six. All right? I could return two, no matter what I pass into it, I get two back. All right? And so, this can be an arbitrary expression here in the return statement. Let's finally look at this final function here, product. Okay? And again, hopefully I picked a name that gives you some indication of what's going to happen. I'm going to return the product of the inputs. All right? So it takes three arguments here, value one, value two, and value three, and returns the product of these three input values. All right? Now notice, I finally have a function that is not one line long here. Let's see what happens. I pass it three numbers, I assign the result to a variable called "result" and then print it. And, we get out the number minus 111.72. Let's look into the body of this function. And as I said before, you can have any code that you can put anywhere else, any Python code that you can put anywhere else can be inside a function. So, I'm going to have variables here. I have a variable called "prod", and I assign the result of multiplying value one times value two, okay? Then I want to update it because I had to, actually had to also multiply it by value three. So I say prod equals prod times value three. All right? So prod at this point, is the product of the first two numbers, I multiply it by the third number, and then assign the result there to prod. But it's that I'm changing the value of the variable at this point, and then I return prod. Okay? So that whatever the value of the prod is at that point, I'm returning it. That is what the function product is going to be evaluated to. So down here, when I call the product, I get the result that was returned to me from the function. I assign that to a variable called "result", we can use it. Again, this can be part of an expression. You know, I can times 54.3, if I want. Okay? And then changes are our answer here and they get printed out. All right? It does not change what the function returns but it changed how I use the return value of the function. Let's take a quicker side now. Take a look at that last line here, where it says, "Print result." That's what that looks like. I have the word "print", then i have an open parenthesis then I have, you know, a value result, and then a close parentheses. What does that look like? It very much looks like a function call. Well, print is a function. Okay, we haven't said that up until now. But now I want to point this out, now that we have seen how to call functions, how to write functions. Python actually provides a built in function called "print" right? And when you write "print", you're actually calling that function. Now this print function is a little bit different than the functions that I just showed you how to write. Right? Because I also have shown you other situations in which we could pass more than one argument to print. Right? We could say, "print" you know, "results come", or "result come" or "result". All right? And it will all just work. In fact, let's do that. Oops! Got an extra closed parenthesis there, run. And now it prints it three times. Okay? So, other than the fact that somehow you can pass any number of arguments you'd like to print, print acts just like the functions that we're writing here ourselves. All right. So print is in fact a function, it's just a special kind of function that takes a variable number of arguments. All right, let's review the elements of a function definition. I start with the word "def", then I give the function name, then I have an open parentheses, I have whatever parameters I have separated by commas. You know, so the first parameter comma, second parameter. Close parentheses when I'm done, and then I need a colon. Don't forget the colon. This is critically important. You have to have the colon to end that first line of the function definition. Then I have to start indenting. Critically important, Python cares about indentation. I start indenting. You can actually indent by any amount but the standard is, you indent by exactly four spaces. And then I start my docstring. Okay. My docstring should describe what the function does. And then I have the code, that is the body of the function, all still indented by exactly four spaces. Each line is indented by four spaces, when that no longer becomes true, you're indicating to Python that the function is over. The code that is in the body of the function can be any Python code that you would put anywhere else. All right? And, if you want to return a value from the function, okay, back to the caller, you have a return statement which can take anything, any expression there. It will evaluate the expression, return it and then that becomes the value used by the caller in place of the function call. Right? A little bit confusing maybe, a lot to take in, but, trust me, you'll get the hang of it.