[MUSIC] Up to this point, we've just been writing code and it basically executes immediately. There's a more powerful construct in programming called the function, where you write the code, and it doesn't execute exactly where it sits in a program, rather it waits until you call it later in your program. When you call it, it then executes the body of the function. More powerfully, you can call the function over and over again. So this allows me to write a piece of code that I can then reuse, I can use it again and again throughout my program. So I write it once and use it multiple times. This is an extremely powerful concept that allows us to write better and more compact programs. Before we get started in talking about how you do this in Python, I want to talk conceptually about what a function is. So let's go ahead and do that. I like to think of a function as a box, so let's draw a box here. Now, this box does something, so let's give it a name that indicates what it does. In this case, we're going to write a function here that compute the area of the triangle, so let's call it triangle area. So we have our box which represents our function, we've call it triangle area. Now, we need something, to compute the triangle area, we need to have some inputs. All right, so we've got to have some inputs coming into our box here. And those inputs are the base, the triangle and the height of the triangle. And something happens inside here. We're writing a function because we want to compute something. So some computation happens. And this produces the area. Now, this isn't very useful to us if it stays inside the box. So I want the area to be an output which comes outside of the box. Now, we have our function, called triangle area, that we can use. So somewhere in our program, we decide we need to know the area of a triangle that has a base of three and a height of two. So we use our box. We pass a 3 and a 2 here. And hopefully, you remember the formula for the area of a triangle. But if you don't, it's one-half times base times height. So this magic box here will produce the number 3, great. Now, more interestingly, we can keep doing this. Notice that these numbers are not inside the box. If we need this again, later in our program, we say we have a triangle that has a base of 4 and a height of 7. We pass that into the box, and out comes the number 14 on the other side. And so we've reused this. We now can compute the area of any triangle that we want, simply by using this box. And note that we don't really care how it happens. So we don't care what this squiggle does. All we care about is that if I give it the correct inputs of base and height, it will give me out the area of the triangle. Now, the other part of this is I actually have to build this function. So well, when I use it, I don't care what the squiggle is. When I'm actually trying to create it, I do have to care, right? This is me now deciding how do I compute the area of a triangle? So inside this box, what is actually happening is there will be something that looks like area = one-half times base times height. And then the value of the area goes over here and gets spit out of the box, right? And the value of the base goes there in the formula. And the value of the height goes there in the formula, all right? So I can write arbitrarily complicated expressions in sine mode of this function, okay, when I'm actually trying to implement it. And you can imagine that I can write any old code that I want, that does whatever I want, in order to compute what I'm supposed to be computing for this function. I want to make another point about the power of functions. So let's imagine that we created some complicated function, or maybe our friend did it for us. We didn't know how to do it, and somebody else created this complicated function. I'm just going to call it f here. All right, we know what it does, but for the purposes of the example, we don't care. All right, f takes two inputs, and it produces some output. And let's say it took a long time to write. It was very complicated. But we know that it does what it does correctly. I now have to write another function. So I'm writing my own function here. Let's call it g, and we take two inputs, and we produce an output as well. And let's call our input a and b here, and we're doing things that we go c = a + b. And then we realize, I need to do something more complicated, and f already does it. So there is no why we can't use functions within functions, right? So we can take c here, and pass it up as an input over here. Let's say b is the other input, that goes over here. And you notice that d, to be the output of f. And now, I can pass d. Let's say d is the final output we have, out of my function g. And so I've got a very powerful thing here. When I'm writing another function, I can use the functions that I already have. And in fact, you should, this is pretty much the whole point of functions, is allowing you to reuse code whenever you can, right? I write my function once, and I call it many times, right? I called my triangle area with different bases and heights. I can now call f with different inputs as well, and it doesn't have to be from outside of a function. When I'm implementing the function if I already have a function that does what I need, I can just use that function that already exists. And this has lots of advantages, you know? First, I don't have to reproduce the code. Let's imagine f was 100 lines of code. I wouldn't have to introduce them inside of g. Also, let's imagine that I find out later that f actually is broken and I fix it. If I'm using f inside of g, g automatically gets fixed. If instead, I had reproduced all of the logic inside of g, well, I'd have to figure out how to fix it there too, and I might forget to do that, all right? So functions are a very powerful concept here. And hopefully, this wets your appetite for learning about how to use them. Now, you have a conceptual understanding of what a function is. We don't know how to use it yet in a program, but at least we have an understanding of why we might want these functions. So now, we have to go off, and we need to see how we both call these functions and write these functions in Python. And that's what we're going to do in the next videos.