How to Think Like a Computer Scientist: Learning with Python 2nd Edition/Fruitful functions

= Fruitful functions =

Return values
The built-in functions we have used, such as abs, pow, and max, have produced results. Calling each of these functions generates a value, which we usually assign to a variable or use as part of an expression.

But so far, none of the functions we have written has returned a value.

In this chapter, we are going to write functions that return values, which we will call fruitful functions, for want of a better name. The first example is area, which returns the area of a circle with the given radius:

We have seen the return statement before, but in a fruitful function the return statement includes a return value. This statement means: Return immediately from this function and use the following expression as a return value. The expression provided can be arbitrarily complicated, so we could have written this function more concisely:

On the other hand, temporary variables like temp often make debugging easier.

Sometimes it is useful to have multiple return statements, one in each branch of a conditional. We have already seen the built-in abs, now we see how to write our own:

Since these <tt>return</tt> statements are in an alternative conditional, only one will be executed. As soon as one is executed, the function terminates without executing any subsequent statements.

Another way to write the above function is to leave out the <tt>else</tt> and just follow the <tt>if</tt> condition by the second <tt>return</tt> statement.

Think about this version and convince yourself it works the same as the first one.

Code that appears after a <tt>return</tt> statement, or any other place the flow of execution can never reach, is called dead code.

In a fruitful function, it is a good idea to ensure that every possible path through the program hits a <tt>return</tt> statement. The following version of <tt>absolute_value</tt> fails to do this:

This version is not correct because if <tt>x</tt> happens to be 0, neither condition is true, and the function ends without hitting a <tt>return</tt> statement. In this case, the return value is a special value called None:

<tt>None</tt> is the unique value of a type called the <tt>NoneType</tt>:

All Python functions return <tt>None</tt> whenever they do not return another value.

Program development
At this point, you should be able to look at complete functions and tell what they do. Also, if you have been doing the exercises, you have written some small functions. As you write larger functions, you might start to have more difficulty, especially with runtime and semantic errors.

To deal with increasingly complex programs, we are going to suggest a technique called incremental development. The goal of incremental development is to avoid long debugging sessions by adding and testing only a small amount of code at a time.

As an example, suppose you want to find the distance between two points, given by the coordinates (x1, y1) and (x2, y2). By the Pythagorean theorem, the distance is:

The first step is to consider what a <tt>distance</tt> function should look like in Python. In other words, what are the inputs (parameters) and what is the output (return value)?

In this case, the two points are the inputs, which we can represent using four parameters. The return value is the distance, which is a floating-point value.

Already we can write an outline of the function:

Obviously, this version of the function doesn't compute distances; it always returns zero. But it is syntactically correct, and it will run, which means that we can test it before we make it more complicated.

To test the new function, we call it with sample values:

We chose these values so that the horizontal distance equals 3 and the vertical distance equals 4; that way, the result is 5 (the hypotenuse of a 3-4-5 triangle). When testing a function, it is useful to know the right answer.

At this point we have confirmed that the function is syntactically correct, and we can start adding lines of code. After each incremental change, we test the function again. If an error occurs at any point, we know where it must be --- in the last line we added.

A logical first step in the computation is to find the differences x2- x1and y2- y1. We will store those values in temporary variables named <tt>dx</tt> and <tt>dy</tt> and print them.

If the function is working, the outputs should be 3 and 4. If so, we know that the function is getting the right parameters and performing the first computation correctly. If not, there are only a few lines to check.

Next we compute the sum of squares of <tt>dx</tt> and <tt>dy</tt>:

Notice that we removed the <tt>print</tt> statements we wrote in the previous step. Code like that is called scaffolding because it is helpful for building the program but is not part of the final product.

Again, we would run the program at this stage and check the output (which should be 25).

Finally, using the fractional exponent <tt>0.5</tt> to find the square root, we compute and return the result:

If that works correctly, you are done. Otherwise, you might want to print the value of <tt>result</tt> before the return statement.

When you start out, you should add only a line or two of code at a time. As you gain more experience, you might find yourself writing and debugging bigger chunks. Either way, the incremental development process can save you a lot of debugging time.

The key aspects of the process are:


 * 1) Start with a working program and make small incremental changes. At any point, if there is an error, you will know exactly where it is.
 * 2) Use temporary variables to hold intermediate values so you can output and check them.
 * 3) Once the program is working, you might want to remove some of the scaffolding or consolidate multiple statements into compound expressions, but only if it does not make the program difficult to read.

Composition
As you should expect by now, you can call one function from within another. This ability is called composition.

As an example, we'll write a function that takes two points, the center of the circle and a point on the perimeter, and computes the area of the circle.

Assume that the center point is stored in the variables <tt>xc</tt> and <tt>yc</tt>, and the perimeter point is in <tt>xp</tt> and <tt>yp</tt>. The first step is to find the radius of the circle, which is the distance between the two points. Fortunately, we've just written a function, <tt>distance</tt>, that does just that, so now all we have to do is use it:

The second step is to find the area of a circle with that radius and return it. Again we will use one of our earlier functions:

Wrapping that up in a function, we get:

We called this function <tt>area2</tt> to distinguish it from the <tt>area</tt> function defined earlier. There can only be one function with a given name within a given module.

The temporary variables <tt>radius</tt> and <tt>result</tt> are useful for development and debugging, but once the program is working, we can make it more concise by composing the function calls:

Boolean functions
Functions can return boolean values, which is often convenient for hiding complicated tests inside functions. For example:

The name of this function is <tt>is_divisible</tt>. It is common to give boolean functions names that sound like yes/no questions. <tt>is_divisible</tt> returns either <tt>True</tt> or <tt>False</tt> to indicate whether the <tt>x</tt> is or is not divisible by <tt>y</tt>.

We can make the function more concise by taking advantage of the fact that the condition of the <tt>if</tt> statement is itself a boolean expression. We can return it directly, avoiding the <tt>if</tt> statement altogether:

This session shows the new function in action:

Boolean functions are often used in conditional statements:

It might be tempting to write something like:

but the extra comparison is unnecessary.

The <tt>function</tt> type
A function is another type in Python, joining <tt>int</tt>, <tt>float</tt>, <tt>str</tt>, <tt>bool</tt>, and <tt>NoneType</tt>.

Just like the other types, functions can be passed as arguments to other functions:

<tt>doto</tt> is called three times. 7 is the argument for value each time, and the functions <tt>f</tt>, <tt>g</tt>, and <tt>h</tt> are passed in for <tt>func</tt> in turn. The output of this script is:

15 37 3 This example is a bit contrived, but we will see situations later where it is quite useful to pass a function to a function.

Programming with style
Readability is very important to programmers, since in practice programs are read and modified far more often then they are written. All the code examples in this book will be consistent with the Python Enhancement Proposal 8 (PEP 8_), a style guide developed by the Python community.

We'll have more to say about style as our programs become more complex, but a few pointers will be helpful already:


 * use 4 spaces for indentation
 * imports should go at the top of the file
 * separate function definitions with two blank lines
 * keep function definitions together
 * keep top level statements, including function calls, together at the bottom of the program

Triple quoted strings
In addition to the single and double quoted strings we first saw in :ref:`values_n_types`, Python also has triple quoted strings:

Triple quoted strings can contain both single and double quotes inside them:

Finally, triple quoted strings can span multiple lines:

Unit testing with <tt>doctest</tt>
It is a common best practice in software development these days to include automatic unit testing of source code. Unit testing provides a way to automatically verify that individual pieces of code, such as functions, are working properly. This makes it possible to change the implimentation of a function at a later time and quickly test that it still does what it was intended to do.

Python has a built-in <tt>doctest</tt> module for easy unit testing. Doctests can be written within a triple quoted string on the first line of the body of a function or script. They consist of sample interpreter sessions with a series of inputs to a Python prompt followed by the expected output from the Python interpreter.

The <tt>doctest</tt> module automatically runs any statement beginning with &gt;&gt;&gt; and compares the following line with the output from the interpreter.

To see how this works, put the following in a script named <tt>myfunctions.py</tt>:

The last three lines are what make <tt>doctest</tt> run. Put them at the bottom of any file that includes doctests. We will explain how they work in Chapter 10 when we discuss modules.

Running the script will produce the following output:

$ python myfunctions.py File &quot;myfunctions.py&quot;, line 3, in __main__.is_divisible_by_2_or_5 Failed example: is_divisible_by_2_or_5(8) Expected: True Got nothing 1 items had failures: 1 of  1 in __main__.is_divisible_by_2_or_5 $ This is an example of a failing test. The test says: if you call <tt>is_divisible_by_2_or_5(8)</tt> the result should be <tt>True</tt>. Since <tt>is_divisible_by_2_or_5</tt> as written doesn't return anything at all, the test fails, and doctest tells us that it expected <tt>True</tt> but got nothing.
 * Test Failed*** 1 failures.

We can make this test pass by returning <tt>True</tt>:

If we run it now, there will be no output, which indicates that the test passed. Note again that the doctest string must be placed immediately after the function definition header in order to run.

To see more detailed out put, call the script with the <tt>-v</tt> command line option:

$ python myfunctions.py -v Trying: is_divisible_by_2_or_5(8) Expecting: True ok 1 items had no tests: __main__ 1 items passed all tests: 1 tests in __main__.is_divisible_by_2_or_5 1 tests in 2 items. 1 passed and 0 failed. Test passed. $ While the test passed, our test suite is clearly inadequate, since <tt>is_divisible_by_2_or_5</tt> will now return <tt>True</tt> no matter what argument is passed to it. Here is a completed version with a more complete test suite and code that makes the tests pass:

Run this script now with the <tt>-v</tt> command line option and see what you get.

Exercises
All of the exercises below should be added to a file named <tt>ch05.py</tt> that contains the following at the bottom:

After completing each exercise in turn, run the program to confirm that the doctests for your new function pass.

<ol> <li> Write a <tt>compare</tt> function that returns <tt>1</tt> if <tt>a &gt; b</tt>, <tt>0</tt> if <tt>a == b</tt>, and <tt>-1</tt> if <tt>a &lt; b</tt>.

Fill in the body of the function so the doctests pass.</li> <li> Use incremental development to write a function called <tt>hypotenuse</tt> that returns the length of the hypotenuse of a right triangle given the lengths of the two legs as parameters. Record each stage of the incremental development process as you go.

When you are finished add your completed function with the doctests to <tt>ch05.py</tt> and confirm that the doctests pass.</li> <li> Write a function <tt>slope(x1, y1, x2, y2)</tt> that returns the slope of the line through the points (x1, y1) and (x2, y2). Be sure your implementation of <tt>slope</tt> can pass the following doctests:

Then a call to <tt>slope</tt> in a new function named <tt>intercept(x1, y1, x2, y2)</tt> that returns the y-intercept of the line through the points <tt>(x1, y1)</tt> and <tt>(x2, y2)</tt>.

<tt>intercept</tt> should pass the doctests above.</li> <li> Write a function called <tt>is_even(n)</tt> that takes an integer as an argument and returns <tt>True</tt> if the argument is an even number and <tt>False</tt> if it is odd. Add your own doctests to this function.</li></ol>

<dl> <dt>#. Now write the function <tt>is_odd(n)</tt> that returns <tt>True</tt> when <tt>n</tt> is odd</dt> <dd> and <tt>False</tt> otherwise. Include doctests for this function as you write it. Finally, modify it so that it uses a call to <tt>is_even</tt> to determine if its argument is an odd integer. </dd> <dt>#.</dt> <dd> Add a body to <tt>is_factor</tt> to make the doctests pass. </dd> <dt>#.</dt> <dd> Add a body to <tt>is_multiple</tt> to make the doctests pass. Can you find a way to use <tt>is_factor</tt> in your definition of <tt>is_multiple</tt>? </dd> <dt>#.</dt> <dd> Write a body for the function definition of <tt>f2c</tt> designed to return the integer value of the nearest degree Celsius for given tempurature in Fahrenheit. (hint: you may want to make use of the built-in function, <tt>round</tt>. Try printing <tt>round.__doc__</tt> in a Python shell and experimenting with round until you are comfortable with how it works.) </dd> <dt>#.</dt> <dd> Add a function body for <tt>c2f</tt> to convert from Celsius to Fahrenheit. </dd></dl>