• No results found

A function is another type in Python, joining int, float, str, bool, and NoneType.

>>> def func():

... return "function func was called..." ...

>>> type(func) <type ’function’> >>>

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

def f(n): return 3*n - 6 def g(n): return 5*n + 2 def h(n): return -2*n + 17 def doto(value, func):

return func(value)

print doto(7, f)

print doto(7, g)

print doto(7, h)

dotois called three times. 7 is the argument for value each time, and the functions f, g, and h are passed in for func 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.

7.7

Glossary

fruitful function: A function that yields a return value.

return value: The value provided as the result of a function call.

temporary variable: A variable used to store an intermediate value in a complex calculation.

dead code: Part of a program that can never be executed, often because it appears after a return statement.

None: A special Python value returned by functions that have no return statement, or a return statement without an argument. None is the only value of the type, NoneType.

incremental development: A program development plan intended to simplify debugging by adding and testing only a small amount of code at a time.

scaffolding: Code that is used during program development but is not part of the final version.

boolean function: A function that returns a boolean value.

composition (of functions): Calling one function from within the body of another, or using the return value of one function as an argument to the call of another.

7.8

Laboratory exercises

1. Write a function called is even(n) that takes an integer as an argument and returns True if the argument is an even number and False if it is odd .

2. Now write the function is odd(n) that returns True when n is odd and False otherwise. Finally, modify it so that it uses a call to is even to determine if its argument is an odd integer.

3. Write a function called is factor which returns True if the first argument is a factor of the second. That is, is factor(3, 12) is True whereas is factor(5, 12) is False.

4. Write a function called hypotenuse that returns the length of the hypotenuse of a right angled trian- gle given the length of the other two sides as parameters. Here’s an initial definition:

def hypotenuse(a,b):

return 0.0

5. Extension Exercise: Recall that the equation for a line can be written as y = mx+c, where m is the slope of the line and c is the y-intercept. For a line that passes through two points, (x1, y1)and (x2, y2), we have the following identities:

m = y2− y1 x2− x1 c = y1− mx1

(a) Write a function slope(x1, y1, x2, y2) that returns the slope of the line through the points (x1, y1)and (x2, y2).

(b) Write a function intercept(x1, y1, x2, y2) that returns the y-intercept of the line through the points (x1, y1) and (x2, y2).

6. If you didn’t complete all the levels of the game in the previous lab try to finish it this time.

Lecture 8

Test driven development

8.1

Modules, files and the import statement

You have already used (without explanation) an import statement. What does an import statement do? Well it imports all or part of a Python module. And what is a module, I hear you ask? A module is simply a Python file that contains definitions (usually function definitions). As programs get large, it makes sense to separate the program into different modules. This allows the programmer to minimize the complexity of the programming task. Typically, similar functionality or related functions are grouped into a single module and separate modules are as independent of each other as possible. Creating your own modules is as easy as creating a Python script with definitions in it.

The Python standard library contains a very large number of modules for many different tasks. It is worth- while browsing the library just to get a feel for all the useful things it can do. The current documentation can be accessed at: http://www.python.org/doc/current/library/index.html, or by choosing Help →Python Docs ... in IDLE. We will look at the doctest module later in this lecture, and throughout the course, several other modules will be introduced.

There are three ways of using import to import a module and subsequently use what was imported. We will use the math module to illuminate:

1.

import math

print math.cos(math.pi/2.0)

This method imports everything from the math module, and members of the module are accessed using dot notation.

2.

from math import cos

print cos(3.14159265/2.0)

This method imports only the definition of cos from the math library. Nothing else is imported. 3.

from math import cos, pi

print cos(pi/2.0)

This method imports only the definitions of cos and pi from the math library. Nothing else is im- ported.

4.

from math import * print cos(pi/2.0)

This method also imports everything from the math module, the difference being that dot notation is not needed to access module members.

It is more common to use the first or second method than the last. The first method has the advantage of avoiding naming conflicts (two different modules defining cos for example), at the cost of lengthier function invocations. The second method has the opposite advantages and disadvantages.