The lectures for the week of Oct 26 will introduce the idea of breaking a task down into functions. Good use of functions can make your programs shorter and easier to write.
You have already used built-in functions such as math.sqrt and string.split. In most of your assignments you have created one user-defined function called main. Functions that do not not take arguments (parameters) let you repeat actions without having to write all the code several times.
Suppose you want your program to print "Help, I'm drowning!" on one line, and "Save me!" on the next, three times. Instead of writing
print "Help, I'm drowning!"
print "Save me!"
print "Help, I'm drowning!"
print "Save me!"
print "Help, I'm drowning!"
print "Save me!"
you could write the following:
def help():
print "Help, I'm drowning!"
print "Save me!"
def main():
help()
help()
help()
Problem 1: Make a table comparing the number of lines of code that need to be written (a) not using a function and (b) using a function for the example above, where we vary the number of times the two lines of text output are repeated. Start by counting (we have filled out line 3 for you) and once you have found the pattern, fill out the rest of the table. Sketch a graph of (a) and (b), plotting Lines of Code (x axis) versus Number Pairs (y axis). What kind of graphs are these: are they linear (straight lines)? How many pairs do you need to print until using a function makes the code shorter?
Number of Help/Save Me pairs printed |
Lines of code not using function |
Lines of code using a function |
1 | ||
2 | ||
3 | 6 | 7 |
4 | ||
5 | ||
6 | ||
7 | ||
8 | ||
16 | ||
32 | ||
64 | ||
128 |
Problem 2: One function can call another function. Consider the following code:
def help():
print "Help, I'm drowning!"
print "Save me!"
def morehelp():
help()
help()
def main():
morehelp()
morehelp()
(a) How many pairs are printed out by this program?
(b) Suppose we changed the program so that morehelp calls help 3 times, and main calls morehelp 3 times. How many pairs would be printed?
(c) Fill out the following table. For the last line of the table, determine what the pattern is, and write expressions using N and M for # Lines of Code and # Help Save Me pairs printed.
(d) Suppose I want to print out 10,000 pairs using as short a program of this form as possible. What should I use for N and M?
# calls of help from morehelp |
# calls of morehelp from main |
# Lines of code in program |
# Help/Save Me pairs printed |
2 | 2 | 9 | |
3 | 3 | ||
2 | 3 | ||
3 | 2 | ||
4 | 4 | ||
4 | 2 | ||
2 | 4 | ||
N | M |
Functions that use parameters are even more useful for writing short code. Consider your current graphics programming assignment. Suppose you have characters represented by graphical objects, such as circles and rectangles. Several times in your screenplay you want a character to hop up and down. If C is a graphical object, you might make it hop using the following commands:
for i in range(5):
C.move(0,2)
time.pause(0.1)
for i in range(5):
C.move(0,-2)
Rather than repeating this code several times in your program, you could define a function:
def hop(Object):
for i in range(5):
Object.move(0,2)
time.pause(0.1)
for i in range(5):
Object.move(0,-2)
Now your main program can make various objects hop easily:
def main()
w = GraphWin()
C = Circle(Point(100,110), 10)
R = Rectangle(Point(200,100), Point(220,120))
C.draw(w)
R.draw(w)
hop(C)
hop(R)
hop(C)
hop(R)
When the function hop is called, the value of the parameter (namely, the graphical object stored in C or stored in R) is placed in the variable Object in the function hop. So, when when the function tells Object to move, the correct original graphical object is moved.
Problem 3: Define functions that perform the following tasks:
(a) walk(Object, Distance, Speed) should animate a graphical Object moving horizontally Distance units at the specified Speed (in units per move method called).
(b) talk(Object, Speech, w) should make a text object appear 20 units directly over the center of Object in window w. (Recall the getCenter method.) The text is specified by the string Speech. The program should pause for 2 seconds, and then erase the text.