2016 September 16,

CS 111: User-Defined Functions

In the preceding section, we caught our first glimpse of functions. In this section, we learn how to write our own functions. After if statements, writing functions is the next huge step toward creating interesting programs.

For our first example, here is some code for convert temperatures from Celsius to Fahrenheit.

cels = 20.0
fahr = 1.8 * cels + 32.0
print(fahr)

This code is in three parts. The first part, cels = 20.0, is the input section; this is where you enter the number that you'd like to convert. The second part, fahr = 1.8 * cels + 32.0, is the core of the code, where the computation gets done. The third part, print(fahr), is the output section; this is where the result of the computation is outputted to the user. When the input number is 20.0, the output number is 68.0. When the input number is 40.0, the output number is 104.0.

We're going to re-write this code as a function, that takes a single number as input, does the required computation, and produces the correct number as output. Here's the code, followed by a short demonstration.

def fahrenheit(cels):
    fahr = 1.8 * cels + 32.0
    return fahr

Let me explain. The first line starts defining (def) a new function called fahrenheit. The function takes a single input, which is called celsius. The function carries out some computation on the input, storing the result in the variable fahr. It then returns the contents of fahr as its output. Notice that the entire body of the function is indented relative to the def statement, much like the body of a while loop (or for loop, or if statement) is indented.

Once we have defined the function fahrenheit using the def statement, we can use that function as many times as we want. In TextWrangler, type the function definition above, and the code below, into a single program file. Run the program.

print(fahrenheit(100.0))
print(fahrenheit(20.0))
print(type(fahrenheit(20.0)))
print(type(fahrenheit))

It's important to emphasize the distinction between returning a value and printing a value. The function fahrenheit does not print anything; it doesn't have any print statements, right? Instead, it merely reports an answer. The user of the function (who may be you, or your partner, or the grader, or me, or you-a-month-from-now-when-you-no-longer-remember-the-code, or someone else) may choose to print that answer, as in print(fahrenheit(20.0)). Or maybe the user wants to convert a list of temperatures. Then she might do this:

celsiuses = [0.0, 10.0, 30.0, 70.0, 120.0]
fahrenheits = []
for temp in celsiuses:
	fahrenheits = fahrenheits + [fahrenheit(temp)]
print(fahrenheits)

This wouldn't work if fahrenheit printed out its answer instead of returning it. Returning an answer lets it be used in subsequent code; printing an answer shows it to the user, but doesn't let it be used in subsequent code.

In the fahrenheit code above, the variables cels and fahr are "dummy variables". The person writing the function can change these variable names as she likes, without changing the meaning or effect of the function, as long as she is consistent. For example, the following version of the fahrenheit function works exactly the same as the one above.

def fahrenheit(tempInCelsius):
    tempInFahr = 1.8 * tempInCelsius + 32.0
    return tempInFahr

Question 15A: Write a celsius function to convert from Fahrenheit to Celsius. If your code is working correctly, then what should celsius(fahrenheit(20.0)) and fahrenheit(celsius(98.6)) be?

For a second example, let's revisit our friend Paul. Your Paul code might have looked like this:

n = 5
if n % 2 == 0:
    answer = n // 2
else:
    answer = n * 3 + 1
print(answer)

Here's the code repackaged as a useful function:

def paul(n):
    if n % 2 == 0:
        answer = n // 2
    else:
        answer = n * 3 + 1
    return answer

Once we have defined the function paul, we can use that function as many times as we want.

print(paul(26))
print(paul(27))
print(paul(paul(paul(27))))

Maybe the user of our function wants to play out an entire Paul monologue. Here's some code:

n = 5
print(n)
while n > 1:
    n = paul(n)
    print(n)

Question 15B: Write a function called monologueLength, that takes as input a positive integer, and returns as output the length for the Paul monologue started with that integer. Your monologueLength function must call upon the paul function. Therefore your program will have to include the definition of the paul function.