Last updated on 2023-11-10 | Edit this page
- How can I define new functions?
- What’s the difference between defining and calling a function?
- What happens when I call a function?
- Define a function that takes parameters.
- Return a value from a function.
- Test and debug a function.
- Set default values for function parameters.
- Explain why we should divide programs into small, single-purpose functions.
At this point, we’ve seen that code can have Python make decisions about what it sees in our data. What if we want to convert some of our data, like taking a temperature in Fahrenheit and converting it to Celsius. We could write something like this for converting a single number
= 99 fahrenheit_val = ((fahrenheit_val - 32) * (5/9))celsius_val
and for a second number we could just copy the line and rename the variables
= 99 fahrenheit_val = ((fahrenheit_val - 32) * (5/9)) celsius_val = 43 fahrenheit_val2 = ((fahrenheit_val2 - 32) * (5/9))celsius_val2
But we would be in trouble as soon as we had to do this more than a
couple times. Cutting and pasting it is going to make our code get very
long and very repetitive, very quickly. We’d like a way to package our
code so that it is easier to reuse, a shorthand way of re-executing
longer pieces of code. In Python we can use ‘functions’. Let’s start by
defining a function
fahr_to_celsius that converts
temperatures from Fahrenheit to Celsius:
def explicit_fahr_to_celsius(temp): # Assign the converted value to a variable = ((temp - 32) * (5/9)) converted # Return the value of the new variable return converted def fahr_to_celsius(temp): # Return converted value more efficiently using the return # function without creating a new variable. This code does # the same thing as the previous function but it is more explicit # in explaining how the return command works. return ((temp - 32) * (5/9))
The function definition opens with the keyword
followed by the name of the function (
a parenthesized list of parameter names (
temp). The body of the function — the statements
that are executed when it runs — is indented below the definition line.
The body concludes with a
return keyword followed by the
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.
This command should call our function, using “32” as the input and return the function value.
In fact, calling our own function is no different from calling any other function:
print('freezing point of water:', fahr_to_celsius(32), 'C') print('boiling point of water:', fahr_to_celsius(212), 'C')
freezing point of water: 0.0 C boiling point of water: 100.0 C
We’ve successfully called the function that we defined, and we have access to the value that we returned.
Now that we’ve seen how to turn Fahrenheit into Celsius, we can also write the function to turn Celsius into Kelvin:
def celsius_to_kelvin(temp_c): return temp_c + 273.15 print('freezing point of water in Kelvin:', celsius_to_kelvin(0.))
freezing point of water in Kelvin: 273.15
What about converting Fahrenheit to Kelvin? We could write out the formula, but we don’t need to. Instead, we can compose the two functions we have already created:
def fahr_to_kelvin(temp_f): = fahr_to_celsius(temp_f) temp_c = celsius_to_kelvin(temp_c) temp_k return temp_k print('boiling point of water in Kelvin:', fahr_to_kelvin(212.0))
boiling point of water in Kelvin: 373.15
This is our first taste of how larger programs are built: we define basic operations, then combine them in ever-larger 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.
In composing our temperature conversion functions, we created
variables inside of those functions,
refer to these variables as local variables because they no
longer exist once the function is done executing. If we try to access
their values outside of the function, we will encounter an error:
print('Again, temperature in Kelvin was:', temp_k)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-1-eed2471d229b> in <module> ----> 1 print('Again, temperature in Kelvin was:', temp_k) NameError: name 'temp_k' is not defined
If you want to reuse the temperature in Kelvin after you have
calculated it with
fahr_to_kelvin, you can store the result
of the function call in a variable:
= fahr_to_kelvin(212.0) temp_kelvin print('temperature in Kelvin was:', temp_kelvin)
temperature in Kelvin was: 373.15
temp_kelvin, being defined outside any
function, is said to be global.
Inside a function, one can read the value of such global variables:
def print_temperatures(): print('temperature in Fahrenheit was:', temp_fahr) print('temperature in Kelvin was:', temp_kelvin) = 212.0 temp_fahr = fahr_to_kelvin(temp_fahr) temp_kelvin print_temperatures()
temperature in Fahrenheit was: 212.0 temperature in Kelvin was: 373.15
Now that we know how to wrap bits of code up in functions, we can
make our inflammation analysis easier to read and easier to reuse.
First, let’s make a
visualize function that generates our
def visualize(filename): = numpy.loadtxt(fname=filename, delimiter=',') data = matplotlib.pyplot.figure(figsize=(10.0, 3.0)) fig = fig.add_subplot(1, 3, 1) axes1 = fig.add_subplot(1, 3, 2) axes2 = fig.add_subplot(1, 3, 3) axes3 'average') axes1.set_ylabel(=0)) axes1.plot(numpy.mean(data, axis 'max') axes2.set_ylabel(=0)) axes2.plot(numpy.amax(data, axis 'min') axes3.set_ylabel(=0)) axes3.plot(numpy.amin(data, axis fig.tight_layout() matplotlib.pyplot.show()
and another function called
detect_problems that checks
for those systematics we noticed:
def detect_problems(filename): = numpy.loadtxt(fname=filename, delimiter=',') data if numpy.amax(data, axis=0) == 0 and numpy.amax(data, axis=0) == 20: print('Suspicious looking maxima!') elif numpy.sum(numpy.amin(data, axis=0)) == 0: print('Minima add up to zero!') else: print('Seems OK!')
Wait! Didn’t we forget to specify what both of these functions should
return? Well, we didn’t. In Python, functions are not required to
return statement and can be used for the sole
purpose of grouping together pieces of code that conceptually do one
thing. In such cases, function names usually describe what they do,
Notice that rather than jumbling this code together in one giant
for loop, we can now read and reuse both ideas separately.
We can reproduce the previous analysis with a much simpler
= sorted(glob.glob('inflammation*.csv')) filenames for filename in filenames[:3]: print(filename) visualize(filename) detect_problems(filename)
By giving our functions human-readable names, we can more easily read
and understand what is happening in the
for loop. Even
better, if at some later date we want to use either of those pieces of
code again, we can do so in a single line.
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 offset a dataset so that it’s mean value shifts to a user-defined value:
def offset_mean(data, target_mean_value): return (data - numpy.mean(data)) + target_mean_value
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 offset its values to have a mean value of 3:
= numpy.zeros((2, 2)) z print(offset_mean(z, 3))
[[ 3. 3.] [ 3. 3.]]
That looks right, so let’s try
offset_mean on our real
= numpy.loadtxt(fname='inflammation-01.csv', delimiter=',') data print(offset_mean(data, 0))
[[-6.14875 -6.14875 -5.14875 ... -3.14875 -6.14875 -6.14875] [-6.14875 -5.14875 -4.14875 ... -5.14875 -6.14875 -5.14875] [-6.14875 -5.14875 -5.14875 ... -4.14875 -5.14875 -5.14875] ... [-6.14875 -5.14875 -5.14875 ... -5.14875 -5.14875 -5.14875] [-6.14875 -6.14875 -6.14875 ... -6.14875 -4.14875 -6.14875] [-6.14875 -6.14875 -5.14875 ... -5.14875 -5.14875 -6.14875]]
It’s hard to tell from the default output whether the result is correct, but there are a few tests that we can run to reassure us:
print('original min, mean, and max are:', numpy.amin(data), numpy.mean(data), numpy.amax(data)) = offset_mean(data, 0) offset_data print('min, mean, and max of offset data are:', numpy.amin(offset_data), numpy.mean(offset_data), numpy.amax(offset_data))
original min, mean, and max are: 0.0 6.14875 20.0 min, mean, and max of offset data are: -6.14875 2.842170943040401e-16 13.85125
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 offset data isn’t quite zero, but it’s pretty close. We can even go further and check that the standard deviation hasn’t changed:
print('std dev before and after:', numpy.std(data), numpy.std(offset_data))
std dev before and after: 4.613833197118566 4.613833197118566
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:
print('difference in standard deviations before and after:', - numpy.std(offset_data)) numpy.std(data)
difference in standard deviations before and after: 0.0
Everything looks good, and 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:
# offset_mean(data, target_mean_value): # return a new array containing the original data with its mean offset to match the desired value. def offset_mean(data, target_mean_value): return (data - numpy.mean(data)) + target_mean_value
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:
def offset_mean(data, target_mean_value): """Return a new array containing the original data with its mean offset to match the desired value.""" return (data - numpy.mean(data)) + target_mean_value
This is better because we can now ask Python’s built-in help system to show us the documentation for the function:
Help on function offset_mean in module __main__: offset_mean(data, target_mean_value) Return a new array containing the original data with its mean offset to match the desired value.
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:
def offset_mean(data, target_mean_value): """Return a new array containing the original data with its mean offset to match the desired value. Examples -------- >>> offset_mean([1, 2, 3], 0) array([-1., 0., 1.]) """ return (data - numpy.mean(data)) + target_mean_value help(offset_mean)
Help on function offset_mean in module __main__: offset_mean(data, target_mean_value) Return a new array containing the original data with its mean offset to match the desired value. Examples -------- >>> offset_mean([1, 2, 3], 0) array([-1., 0., 1.])
We have passed parameters to functions in two ways: directly, as in
type(data), and by name, as in
numpy.loadtxt(fname='something.csv', delimiter=','). In
fact, we can pass the filename to
loadtxt without the
array([[ 0., 0., 1., ..., 3., 0., 0.], [ 0., 1., 2., ..., 1., 0., 1.], [ 0., 1., 1., ..., 2., 1., 1.], ..., [ 0., 1., 1., ..., 1., 1., 1.], [ 0., 0., 0., ..., 0., 2., 0.], [ 0., 0., 1., ..., 1., 1., 0.]])
but we still need to say
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/username/anaconda3/lib/python3.6/site-packages/numpy/lib/npyio.py", line 1041, in loa dtxt dtype = np.dtype(dtype) File "/Users/username/anaconda3/lib/python3.6/site-packages/numpy/core/_internal.py", line 199, in _commastring newitem = (dtype, eval(repeats)) File "<string>", line 1 , ^ SyntaxError: unexpected EOF while parsing
To understand what’s going on, and make our own functions easier to
use, let’s re-define our
offset_mean function like
def offset_mean(data, target_mean_value=0.0): """Return a new array containing the original data with its mean offset to match the desired value, (0 by default). Examples -------- >>> offset_mean([1, 2, 3]) array([-1., 0., 1.]) """ return (data - numpy.mean(data)) + target_mean_value
The key change is that the second parameter is now written
target_mean_value=0.0 instead of just
target_mean_value. If we call the function with two
arguments, it works as it did before:
= numpy.zeros((2, 2)) test_data print(offset_mean(test_data, 3))
[[ 3. 3.] [ 3. 3.]]
But we can also now call it with just one parameter, in which case
target_mean_value is automatically assigned the default value of 0.0:
= 5 + numpy.zeros((2, 2)) more_data print('data before mean offset:') print(more_data) print('offset data:') print(offset_mean(more_data))
data before mean offset: [[ 5. 5.] [ 5. 5.]] offset data: [[ 0. 0.] [ 0. 0.]]
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:
def display(a=1, b=2, c=3): print('a:', a, 'b:', b, 'c:', c) print('no parameters:') display()print('one parameter:') 55) display(print('two parameters:') 55, 66)display(
no parameters: a: 1 b: 2 c: 3 one parameter: a: 55 b: 2 c: 3 two parameters: a: 55 b: 66 c: 3
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:
print('only setting the value of c') =77)display(c
only setting the value of c a: 1 b: 2 c: 77
With that in hand, let’s look at the help for
Help on function loadtxt in module numpy.lib.npyio: loadtxt(fname, dtype=<class 'float'>, comments='#', delimiter=None, converters=None, skiprows=0, use cols=None, unpack=False, ndmin=0, encoding='bytes') Load data from a text file. Each row in the text file must have the same number of values. Parameters ---------- ...
There’s a lot of information here, but the most important part is the first couple of lines:
loadtxt(fname, dtype=<class 'float'>, comments='#', delimiter=None, converters=None, skiprows=0, use cols=None, unpack=False, ndmin=0, encoding='bytes')
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:
then the filename is assigned to
fname (which is what we
want), but the delimiter string
',' is assigned to
dtype rather than
dtype is the second parameter in the list. However
',' isn’t a known
dtype so our code produced
an error message when we tried to run it. When we call
loadtxt we don’t have to provide
the filename because it’s the first item in the list, but if we want the
',' to be assigned to the variable
we do have to provide
delimiter= for the second
delimiter is not the second parameter in
Consider these two functions:
def s(p): = 0 a for v in p: += v a = a / len(p) m = 0 d for v in p: += (v - m) * (v - m) d return numpy.sqrt(d / (len(p) - 1)) def std_dev(sample): = 0 sample_sum for value in sample: += value sample_sum = sample_sum / len(sample) sample_mean = 0 sum_squared_devs for value in sample: += (value - sample_mean) * (value - sample_mean) sum_squared_devs return numpy.sqrt(sum_squared_devs / (len(sample) - 1))
computationally equivalent (they both calculate the sample standard
deviation), but to a human reader, they look very different. You
std_dev much easier to read and understand
As this example illustrates, both documentation and a programmer’s coding style combine to determine how easy it is for others to read and understand the programmer’s code. Choosing meaningful variable names and using blank spaces to break the code into logical “chunks” are helpful techniques for producing readable code. This is useful not only for sharing code with others, but also for the original programmer. If you need to revisit code that you wrote months ago and haven’t thought about since then, you will appreciate the value of readable code!
“Adding” two strings produces their concatenation:
'a' + 'b' is
'ab'. Write a function called
fence that takes two parameters called
wrapper and returns a new string
that has the wrapper character at the beginning and end of the original.
A call to your function should look like this:
def fence(original, wrapper): return wrapper + original + wrapper
return statement, on the other hand, makes data
visible to the program. Let’s have a look at the following function:
def add(a, b): print(a + b)
Question: What will we see if we execute the following commands?
= add(7, 3) A print(A)
Python will first execute the function
a = 7 and
b = 3, and, therefore, print
10. However, because function
add does not
have a line that starts with
“statement”), it will, by default, return nothing which, in Python
world, is called
A will be
None and the last line (
None. As a result, we will see:
If the variable
s refers to a string, then
s is the string’s first character and
is its last. Write a function called
outer that returns a
string made up of just the first and last characters of its input. A
call to your function should look like this:
def outer(input_string): return input_string + input_string[-1]
def rescale(input_array): = numpy.amin(input_array) L = numpy.amax(input_array) H = (input_array - L) / (H - L) output_array return output_array
Run the commands
help(numpy.linspace) to see how to use these functions to
generate regularly-spaced values, then use those values to test your
rescale function. Once you’ve successfully tested your
function, add a docstring that explains what it does.
"""Takes an array as input, and returns a corresponding array scaled so that 0 corresponds to the minimum and 1 to the maximum value of the input array. Examples: >>> rescale(numpy.arange(10.0)) array([ 0. , 0.11111111, 0.22222222, 0.33333333, 0.44444444, 0.55555556, 0.66666667, 0.77777778, 0.88888889, 1. ]) >>> rescale(numpy.linspace(0, 100, 5)) array([ 0. , 0.25, 0.5 , 0.75, 1. ]) """
def rescale(input_array, low_val=0.0, high_val=1.0): """rescales input array values to lie between low_val and high_val""" = numpy.amin(input_array) L = numpy.amax(input_array) H = (input_array - L) / (H - L) intermed_array = intermed_array * (high_val - low_val) + low_val output_array return output_array
259.81666666666666 278.15 273.15 0
k is 0 because the
k inside the function
f2k doesn’t know about the
k defined outside
the function. When the
f2k function is called, it creates a
k. The function does not return any values and does not
k outside of its local copy. Therefore the original
k remains unchanged. Beware that a local
k is created because
f2k internal statements
affect a new value to it. If
k was only
read, it would simply retrieve the global
Given the following code:
def numbers(one, two=2, three, four=4): = str(one) + str(two) + str(three) + str(four) n return n print(numbers(1, three=3))
what do you expect will be printed? What is actually printed? What rule do you think Python is following?
Given that, what does the following piece of code display when run?
def func(a, b=3, c=6): print('a: ', a, 'b: ', b, 'c:', c) -1, 2)func(
a: b: 3 c: 6
a: -1 b: 3 c: 6
a: -1 b: 2 c: 6
a: b: -1 c: 2
Attempting to define the
numbers function results in
4. SyntaxError. The defined parameters
four are given default values. Because
three are not given default values, they are required to be
included as arguments when the function is called and must be placed
before any parameters that have default values in the function
The given call to
a: -1 b: 2 c: 6. -1 is assigned to the first parameter
a, 2 is assigned to the next parameter
c is not passed a value, so it uses its default value
- Define a function using
- The body of a function must be indented.
- Call a function using
- Numbers are stored as integers or floating-point numbers.
- Variables defined within a function can only be seen and used within the body of the function.
- Variables created outside of any function are called global variables.
- Within a function, we can access global variables.
- Variables created within a function override global variables if their names match.
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=valuein 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).
- Put code whose parameters change frequently in a function, then call it with different parameter values to customize its behavior.