Hi there. In addition to using Python's built-in functions, we can also define our own. And that's what this lecture's all about. Let's start with an example from math. Function f of x, takes x and squares it. Here's how the function looks in Python. [SOUND]. Def indicates to Python that we're defining a function. F is the name of the function. X is call the parameter if the function. It's a variable whose value will be supplied when the function is called. This colon indicates to Python that we're about to type what happens when the function is called. And the word Return Indicates that we're passing back a value. The general form of a return statement is the word return, followed by an expression. And the rules for executing a return statement are, first, evaluate the expression, producing a memory address. And second, pass back that memory address to the caller. The general form of a function definition is the word def, followed by the name of the function and then zero or more parameters separated by comas. Next comes the body of the function, which is one or more statements often ending with a return statement. Notice that the body is indented. Let's call the function f. So we'll pass in an argument 3. When the function is called, the perimeter acts as a [INAUDIBLE] memory address of the value 3. We can think of this, actually, like an assignment statement, where x gets 3. I'll hit Enter, so the function call is executed. And the result of the function call is that the value 9 is returned. Function calls are actually expressions, so we can use a variable to store the result. Let's create a variable result that gets the result of calling f of 3. Recall the rules of executing an assignment statement. Which are that first, the expression on the right-hand side is evaluated, producing a memory address. And second, the memory address is stored in the variable. The result gets the memory address of the call to f of 3. So in other words, result gets 9. Let's define another function. Recall from the variables lecture how to calculate the area of a triangle. Take the base, multiply it by the height, and then divide by 2. We'll now write a function to calculate the area of a triangle. We start with the word def, and next comes the function name, let's use the name area. After the function name and the parameters to the function, in this case we will have two parameters. The base and the height. We end this line with a colon, and then we write the body of the function. We are going to return the base multiplied by the height divided by 2. Let's call the function, we'll execute area passing in two arguments. 3 and 4. And base, that parameter gets 3, the parameter height gets 4. And then that expression is evaluated base times height, divided by 2 and return. So 6.0 is returned by the function. Let's call it one more time, passing in an int and a float this time. The extended rules for executing a function call are to first evaluate, the arguments to produce memory addresses. Next, store those memory addresses in the corresponding parameters. And then execute the body of the function. Now, let's restart the Python shell. We go to the shell menu, and click Restart. And we've just lost all the work that we did in the shell, including the area function definition. If we try to call area, we get a name error, because area is not defined. Most Python programs are saved in files. Let's create a new file, and save our function definition, area, in it. Go to File > New Window. Place our function definition in there. And, we'll save this file as triangle.py. All of our Python programs will be saved in .py files. Just resize the windows, so we can see the shell on the left-hand side, and our triangle.py file on the right-hand side. Now that we have our area function definition in Triangle.py, we can run this file. So we click Run, and Run Module. That makes the area function definition available in the shell. So now, when we call area from the shell, it knows what area is, and is able to execute that function. [SOUND] We can define more than one function in the Triangle.py file. Let's add another function definition. We will define a function to calculate the perimeter of a triangle. We'll pass in sides 1, 2, and 3. And the function will return the sum of those sides [SOUND] At this point, I've saved triangle.py and if I go back to the shell and execute the function, what we'll see is an error. That's because at this point, although the function is saved in triangle.py, we have not run Triangle.py, and so the shell does not know what perimeter is. Before executing a function in the shell, we need to run that module, so I've hit the cmd to run the module, and now I can call perimeter, and it executes.