You are on page 1of 3

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(x) takes x and squares it. Here is how the function looks in python: "def" indicates to Python that we're defining a function. "f" is the name of the function. "x" is called a parameter of 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 the 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 with commas. Next comes the body of the function, which is one or more statements often ending with the return statement. Notice that the body is indented. Let's call the function "f". We'll pass in an argument, three. When the function is called, the parameter x is assigned the memory address of the value three. We can think of this actually like an assignment statement, where x gets the memory address of three. I'll hit enter so the function call is executed, and the result of the function call is that the value nine 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(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(3) so in other words, result gets the memory address of nine. 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 two. 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 come 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 two. Let's call the function. We'll execute "area" passing in two arguments, 3 and 4... and base -- that parameter -- gets the memory address of 3. The parameter height gets the memory address of 4. And then that expression is evaluated, base times height divided by two and returned. 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 expanded 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. Go to the shell menu, and click restart. And we've just lost all of 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. Resize the windows so that 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.

We can define more than one function in the triangle.py file. Let's add another function definition. We'll define a function to calculate the perimeter of a triangle. We'll pass in sides one, two and three and the function will return the sum of those sides. 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 command to run the module. And now I can call "perimeter," and it executes.