## Function Basics

Earlier in this chapter, we converted 80 degrees Fahrenheit to Celsius. A mathematician would write this as Jt)=§ (t-32), where t is the temperature in Fahrenheit that we want to convert to Celsius. To find out what 80 degrees Fahrenheit is in Celsius, we replace t with 80, which gives us J(80) = § (80-32), or 26§.

We can write functions in Python, too. As in mathematics, they are used to define common formulas. Here is the conversion function in Python:

It has these major differences from its mathematical equivalent:

• A function definition is another kind of Python statement; it defines a new name whose value can be rather complicated but is still just a value.

• The keyword def is used to tell Python that we're defining a new function.

• We use a readable name like to_celsius for the function rather than something like f whose meaning will be hard to remember an hour later. (This isn't actually a requirement, but it's good style.)

• There is a colon instead of an equals sign.

• The actual formula for the function is defined on the next line. The line is indented four spaces and marked with the keyword return.

Python displays a triple-dot prompt automatically when you're in the middle of defining a new function; you do not type the dots any more than you type the greater-than signs in the usual >>> prompt. If you're using a smart editor, like the one in Wing 101, it will automatically indent the body of the function by the required amount. (This is another reason to use Wing 101 instead of a basic text editor like Notepad or Pico: it saves a lot of wear and tear on your spacebar and thumb.)

Here is what happens when we ask Python to evaluate to_celsius(80), to_celsius(78.8), and to_celsius(10.4):

>>> to_celsius(80) 26.666666666666668 >>> to_celsius(78.8) 26.0

Each of these three statements is called a function call, because we're calling up the function to do some work for us. We have to define a function only once; we can call it any number of times.

The general form of a function definition is as follows:

def function_name(parameters): block

As we've already seen, the def keyword tells Python that we're defining a new function. The name of the function comes next, followed by zero or more parameters in parentheses and a colon. A parameter is a variable (like t in the function to_celsius) that is given a value when the function is called. For example, 80 was assigned to t in the function call to_celsius(80), and then 78.8 in to_celsius(78.8), and then 10.4 in to_celsius(10.4). Those actual values are called the arguments to the function.

What the function does is specified by the block of statements inside it. to_celsius's block consisted of just one statement, but as we'll see later, the blocks making up more complicated functions may be many statements long.

Finally, the return statement has this general form: return expression def to_celsius(t):

to celsius(80)

Q (rest of program)

Figure 2.8: Function control flow and is executed as follows:

1. Evaluate the expression to the right of the keyword return.

### 2. Use that value as the result of the function.

It's important to be clear on the difference between a function definition and a function call. When a function is defined, Python records it but doesn't execute it. When the function is called, Python jumps to the first line of that function and starts running it (see Figure 2.8). When the function is finished, Python returns to the place where the function was originally called.

### Local Variables

Some computations are complex, and breaking them down into separate steps can lead to clearer code. Here, we break down the evaluation of the polynomial ax2 + bx+ c into several steps:

Variables like first, second, and third that are created within a function are called local variables. These variables exist only during function execution; when the function finishes executing, the variables no longer exist. This means that trying to access a local variable from outside the

first = a * x * x second = b * x third = c return first + second + third function is an error, just like trying to access a variable that has never been defined:

>>> polynomial(2, 3, 4, 1.3) 11.280000000000001 >>> first

Traceback (most recent call last):

File "<stdin>", line 1, in <module> NameError: name 'first' is not defined >>> a

Traceback (most recent call last):

File "<stdin>", line 1, in <module> NameError: name 'a' is not defined

As you can see from this example, a function's parameters are also local variables. When a function is called, Python assigns the argument values given in the call to the function's parameters. As you might expect, if a function is defined to take a certain number of parameters, it must be passed the same number of arguments:4