Creating Functions

If we only had one data set to analyze, it would probably be faster to load the file into a spreadsheet and use that to plot some simple statistics. But we have twelve files to check, and may have more in future. In this lesson, we'll learn how to write a function so that we can repeat several operations with a single command.

Objectives

  • Define a function that takes parameters.
  • Return a value from a function.
  • Test and debug a function.
  • Explain what a call stack is, and trace changes to the call stack as functions are called.
  • Set default values for function parameters.
  • Explain why we should divide programs into small, single-purpose functions.

Defining a Function

Let's start by defining a function fahr_to_kelvin that converts temperatures from Fahrenheit to Kelvin:


In [1]:
def fahr_to_kelvin(temp):
    return ((temp - 32) * (5/9)) + 273.15

The definition opens with the word def, which is followed by the name of the function and a parenthesized list of parameter names. The body of the function—the statements that are executed when it runs—is indented below the definition line, typically by four spaces.

When we call the function, the values we pass to it are assigned to those variables so that we can use them inside the function. Inside the function, we use a return statement to send a result back to whoever asked for it.

Let's try running our function. Calling our own function is no different from calling any other function:


In [3]:
print 'freezing point of water:', fahr_to_kelvin(32)
print 'boiling point of water:', fahr_to_kelvin(212)


freezing point of water: 273.15
boiling point of water: 273.15

We've successfully called the function that we defined, and we have access to the value that we returned. Unfortunately, the value returned doesn't look right. What went wrong?

Debugging a Function

Debugging is when we fix a piece of code that we know is working incorrectly. In this case, we know that fahr_to_kelvin is giving us the wrong answer, so let's find out why.

For big pieces of code, there are tools called debuggers that aid in this process.

We just have a short function, so we'll debug by choosing some parameter value, breaking our function into small parts, and printing out the value of each part.


In [3]:
# We'll use temp = 212, the boiling point of water, which was incorrect
print "212 - 32:", 212 - 32


212 - 32: 180

In [4]:
print "(212 - 32) * (5/9):", (212 - 32) * (5/9)


(212 - 32) * (5/9): 0

Aha! The problem comes when we multiply by 5/9. This is because 5/9 is actually 0.


In [5]:
5/9


Out[5]:
0

Computers store numbers in one of two ways: as integers or as floating-point numbers (or floats). The first are the numbers we usually count with; the second have fractional parts. Addition, subtraction and multiplication work on both as we'd expect, but division works differently. If we divide one integer by another, we get the quotient without the remainder:


In [6]:
print '10/3 is:', 10/3


10/3 is: 3

If either part of the division is a float, on the other hand, the computer creates a floating-point answer:


In [7]:
print '10.0/3 is:', 10.0/3


10.0/3 is: 3.33333333333

The computer does this for historical reasons: integer operations were much faster on early machines, and this behavior is actually useful in a lot of situations. It's still confusing, though, so Python 3 produces a floating-point answer when dividing integers if it needs to. We're still using Python 2.7 in this class, though, so if we want 5/9 to give us the right answer, we have to write it as 5.0/9, 5/9.0, or some other variation.

Another way to create a floating-point answer is to explicitly tell the computer that you desire one. This is achieved by casting one of the numbers:


In [8]:
print 'float(10)/3 is:', float(10)/3


float(10)/3 is: 3.33333333333

The advantage to this method is it can be used with existing variables. Let's take a look:


In [9]:
a = 10
b = 3
print 'a/b is:', a/b
print 'float(a)/b is:', float(a)/b


a/b is: 3
float(a)/b is: 3.33333333333

See that was much easier than redefining a or b!

Let's fix our fahr_to_kelvin function with this new knowledge.


In [10]:
def fahr_to_kelvin(temp):
    return ((temp - 32) * (5.0/9.0)) + 273.15

print 'freezing point of water:', fahr_to_kelvin(32)
print 'boiling point of water:', fahr_to_kelvin(212)


freezing point of water: 273.15
boiling point of water: 373.15

It works!

Composing Functions

Now that we've seen how to turn Fahrenheit into Kelvin, it's easy to turn Kelvin into Celsius:


In [11]:
def kelvin_to_celsius(temp):
    return temp - 273.15

print 'absolute zero in Celsius:', kelvin_to_celsius(0.0)


absolute zero in Celsius: -273.15

What about converting Fahrenheit to Celsius? We could write out the formula, but we don't need to. Instead, we can compose the two functions we have already created:


In [12]:
def fahr_to_celsius(temp):
    temp_k = fahr_to_kelvin(temp)
    result = kelvin_to_celsius(temp_k)
    return result

print 'freezing point of water in Celsius:', fahr_to_celsius(32.0)


freezing point of water in Celsius: 0.0

This is our first taste of how larger programs are built: we define basic operations, then combine them in ever-large chunks to get the effect we want. Real-life functions will usually be larger than the ones shown here—typically half a dozen to a few dozen lines—but they shouldn't ever be much longer than that, or the next person who reads it won't be able to understand what's going on.

Challenges

  1. "Adding" two strings produces their concatenation: 'a' + 'b' is 'ab'. Write a function called fence that takes two parameters called original and wrapper and returns a new string that has the wrapper character at the beginning and end of the original:

    print fence('name', '*')
    *name*
    
  2. If the variable s refers to a string, then s[0] is the string's first character and s[-1] is its last. Write a function called outer that returns a string made up of just the first and last characters of its input:

    print outer('helium')
    hm
    

The Call Stack

Let's take a closer look at what happens when we call fahr_to_celsius(32.0). To make things clearer, we'll start by putting the initial value 32.0 in a variable and store the final result in one as well:


In [13]:
original = 32.0
final = fahr_to_celsius(original)

The diagram below shows what memory looks like after the first line has been executed:

When we call fahr_to_celsius, Python doesn't create the variable temp right away. Instead, it creates something called a stack frame to keep track of the variables defined by fahr_to_kelvin. Initially, this stack frame only holds the value of temp:

When we call fahr_to_kelvin inside fahr_to_celsius, Python creates another stack frame to hold fahr_to_kelvin's variables:

It does this because there are now two variables in play called temp: the parameter to fahr_to_celsius, and the parameter to fahr_to_kelvin. Having two variables with the same name in the same part of the program would be ambiguous, so Python (and every other modern programming language) creates a new stack frame for each function call to keep that function's variables separate from those defined by other functions.

When the call to fahr_to_kelvin returns a value, Python throws away fahr_to_kelvin's stack frame and creates a new variable in the stack frame for fahr_to_celsius to hold the temperature in Kelvin:

It then calls kelvin_to_celsius, which means it creates a stack frame to hold that function's variables:

Once again, Python throws away that stack frame when kelvin_to_celsius is done and creates the variable result in the stack frame for fahr_to_celsius:

Finally, when fahr_to_celsius is done, Python throws away its stack frame and puts its result in a new variable called final that lives in the stack frame we started with:

This final stack frame is always there; it holds the variables we defined outside the functions in our code. What it doesn't hold is the variables that were in the various stack frames. If we try to get the value of temp after our functions have finished running, Python tells us that there's no such thing:


In [14]:
print 'final value of temp after all function calls:', temp


final value of temp after all function calls:
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-14-ffd9b4dbd5f1> in <module>()
----> 1 print 'final value of temp after all function calls:', temp

NameError: name 'temp' is not defined

Why go to all this trouble? Well, here's a function called span that calculates the difference between the minimum and maximum values in an array:


In [ ]:
import numpy

def span(a):
    diff = a.max() - a.min()
    return diff

data = numpy.loadtxt(fname='data/inflammation-01.csv', delimiter=',')
print 'span of data', span(data)

Notice that span assigns a value to a variable called diff. We might very well use a variable with the same name to hold data:


In [ ]:
diff = numpy.loadtxt(fname='data/inflammation-01.csv', delimiter=',')
print 'span of data:', span(diff)

We don't expect diff to have the value 20.0 after this function call, so the name diff cannot refer to the same thing inside span as it does in the main body of our program. And yes, we could probably choose a different name than diff in our main program in this case, but we don't want to have to read every line of NumPy to see what variable names its functions use before calling any of those functions, just in case they change the values of our variables.

The big idea here is encapsulation, and it's the key to writing correct, comprehensible programs. A function's job is to turn several operations into one so that we can think about a single function call instead of a dozen or a hundred statements each time we want to do something. That only works if functions don't interfere with each other; if they do, we have to pay attention to the details once again, which quickly overloads our short-term memory.

Challenges

  1. We previously wrote functions called fence and outer. Draw a diagram showing how the call stack changes when we run the following:

    print outer(fence('carbon', '+'))
    

Testing and Documenting

Once we start putting things in functions so that we can re-use them, we need to start testing that those functions are working correctly. To see how to do this, let's write a function to center a dataset around a particular value:


In [ ]:
def center(data, desired):
    return (data - data.mean()) + desired

We could test this on our actual data, but since we don't know what the values ought to be, it will be hard to tell if the result was correct. Instead, let's use NumPy to create a matrix of 0's and then center that around 3:


In [ ]:
z = numpy.zeros((2,2))
print center(z, 3)

That looks right, so let's try center on our real data:


In [ ]:
data = numpy.loadtxt(fname='data/inflammation-01.csv', delimiter=',')
print center(data, 0)

It's hard to tell from the default output whether the result is correct, but there are a few simple tests that will reassure us:


In [ ]:
print 'original min, mean, and max are:', data.min(), data.mean(), data.max()
centered = center(data, 0)
print 'min, mean, and and max of centered data are:', centered.min(), centered.mean(), centered.max()

That seems almost right: the original mean was about 6.1, so the lower bound from zero is now about -6.1. The mean of the centered data isn't quite zero—we'll explore why not in the challenges—but it's pretty close. We can even go further and check that the standard deviation hasn't changed:


In [ ]:
print 'std dev before and after:', data.std(), centered.std()

Those values look the same, but we probably wouldn't notice if they were different in the sixth decimal place. Let's do this instead:


In [ ]:
print 'difference in standard deviations before and after:', data.std() - centered.std()

Again, the difference is very small. It's still possible that our function is wrong, but it seems unlikely enough that we should probably get back to doing our analysis. We have one more task first, though: we should write some documentation for our function to remind ourselves later what it's for and how to use it.

The usual way to put documentation in software is to add comments like this:


In [ ]:
# center(data, desired): return a new array containing the original data centered around the desired value.
def center(data, desired):
    return (data - data.mean()) + desired

There's a better way, though. If the first thing in a function is a string that isn't assigned to a variable, that string is attached to the function as its documentation:


In [ ]:
def center(data, desired):
    '''Return a new array containing the original data centered around the desired value.'''
    return (data - data.mean()) + desired

This is better because we can now ask Python's built-in help system to show us the documentation for the function:


In [ ]:
help(center)

A string like this is called a docstring. We don't need to use triple quotes when we write one, but if we do, we can break the string across multiple lines:


In [1]:
def center(data, desired):
    '''Return a new array containing the original data centered around the desired value.
    Example: center([1, 2, 3], 0) => [-1, 0, 1]'''
    return (data - data.mean()) + desired

help(center)


Help on function center in module __main__:

center(data, desired)
    Return a new array containing the original data centered around the desired value.
    Example: center([1, 2, 3], 0) => [-1, 0, 1]

Challenges

  1. Write a function called analyze that takes a filename as a parameter and displays the three graphs produced in the previous lesson, i.e., analyze('data/inflammation-01.csv') should produce the graphs already shown, while analyze('data/inflammation-02.csv') should produce corresponding graphs for the second data set. Be sure to give your function a docstring.

  2. Write a function rescale that takes an array as input and returns a corresponding array of values scaled to lie in the range 0.0 to 1.0. (If $L$ and $H$ are the lowest and highest values in the original array, then the replacement for a value $v$ should be $(v-L) / (H-L)$.) Be sure to give the function a docstring.

  3. Run the commands help(numpy.arange) and help(numpy.linspace) to see how to use these functions to generate regularly-spaced values, then use those values to test your rescale function.

Defining Defaults

We have passed parameters to functions in two ways: directly, as in span(data), and by name, as in numpy.loadtxt(fname='something.csv', delimiter=','). In fact, we can pass the filename to loadtxt without the fname=:


In [ ]:
numpy.loadtxt('data/inflammation-01.csv', delimiter=',')

but we still need to say delimiter=:


In [ ]:
numpy.loadtxt('data/inflammation-01.csv', ',')

To understand what's going on, and make our own functions easier to use, let's re-define our center function like this:


In [6]:
def center(data, desired=0.0):
    '''Return a new array containing the original data centered around the desired value (0 by default).
    Example: center([1, 2, 3], 0) => [-1, 0, 1]'''
    return (data - data.mean()) + desired

The key change is that the second parameter is now written desired=0.0 instead of just desired. If we call the function with two arguments, it works as it did before:


In [7]:
test_data = numpy.zeros((2, 2))
print center(test_data, 3)


[[ 3.  3.]
 [ 3.  3.]]

But we can also now call it with just one parameter, in which case desired is automatically assigned the default value of 0.0:


In [3]:
import numpy
center(numpy.ndarray([1,2,3]),0)

In [4]:
more_data = 5 + numpy.zeros((2, 2))
print 'data before centering:', more_data
print 'centered data:', center(more_data)


data before centering: [[ 5.  5.]
 [ 5.  5.]]
centered data:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-3c0468d7153f> in <module>()
      1 more_data = 5 + numpy.zeros((2, 2))
      2 print 'data before centering:', more_data
----> 3 print 'centered data:', center(more_data)

TypeError: center() takes exactly 2 arguments (1 given)

This is handy: if we usually want a function to work one way, but occasionally need it to do something else, we can allow people to pass a parameter when they need to but provide a default to make the normal case easier. The example below shows how Python matches values to parameters:


In [ ]:
def display(a=1, b=2, c=3):
    print 'a:', a, 'b:', b, 'c:', c

print 'no parameters:'
display()
print 'one parameter:'
display(55)
print 'two parameters:'
display(55, 66)

As this example shows, parameters are matched up from left to right, and any that haven't been given a value explicitly get their default value. We can override this behavior by naming the value as we pass it in:


In [ ]:
print 'only setting the value of c'
display(c=77)

With that in hand, let's look at the help for numpy.loadtxt:


In [ ]:
help(numpy.loadtxt)

There's a lot of information here, but the most important part is the first couple of lines:

loadtxt(fname, dtype=<type 'float'>, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None,
        unpack=False, ndmin=0)

This tells us that loadtxt has one parameter called fname that doesn't have a default value, and eight others that do. If we call the function like this:

numpy.loadtxt('data/inflammation-01.csv', ',')

then the filename is assigned to fname (which is what we want), but the delimiter string ',' is assigned to dtype rather than delimiter, because dtype is the second parameter in the list. That's why we don't have to provide fname= for the filename, but do have to provide delimiter= for the second parameter.

Challenges (optional)

  1. Rewrite the rescale function so that it scales data to lie between 0.0 and 1.0 by default, but will allow the caller to specify lower and upper bounds if they want. Compare your implementation to your neighbor's: do the two functions always behave the same way?

Key Points

  • Define a function using def name(...params...).
  • The body of a function must be indented.
  • Call a function using name(...values...).
  • Numbers are stored as integers or floating-point numbers.
  • Integer division produces the whole part of the answer (not the fractional part).
  • Each time a function is called, a new stack frame is created on the call stack to hold its parameters and local variables.
  • Python looks for variables in the current stack frame before looking for them at the top level.
  • Use help(thing) to view help for something.
  • Put docstrings in functions to provide help for that function.
  • Specify default values for parameters when defining a function using name=value in the parameter list.
  • Parameters can be passed by matching based on name, by position, or by omitting them (in which case the default value is used).

Next Steps

We now have a function called analyze to visualize a single data set. We could use it to explore all 12 of our current data sets like this:

analyze('data/inflammation-01.csv')
analyze('data/inflammation-02.csv')
...
analyze('data/inflammation-12.csv')

but the chances of us typing all 12 filenames correctly aren't great, and we'll be even worse off if we get another hundred files. What we need is a way to tell Python to do something once for each file, and that will be the subject of the next lesson.