Software Design Spring 2007 For today, you should have: 1) read Chapters 6 and 7 2) started Homework 3 3) prepared for a quiz For next time: 1) read Chapter 8 of "How to think..." Recurtle the Turtle ------------------- On paper, sketch what you think this program will draw, then type it into turtle_code.py and run it from TurtleWorld. def draw(t, dist, n): if n==0: return fd(t, dist) lt(t) draw(t, dist, n-1) world.clear() bob = Turtle(world) draw(bob, 50, 4) Draw a stack diagram that shows the state of the program when n == 0. Recurtle, part 2 ---------------- Modify turtle_code.py so it looks like this: def draw(t, dist, n): if n == 0: return fd(t, dist*n) lt(t, 45) draw(t, dist, n-1) rt(t, 90) draw(t, dist, n-1) lt(t, 45) bk(t, dist*n) world.clear() bob = Turtle(world) bob.delay = 0.1 lt(bob) draw(bob, 10, 3) You might want to adjust the value of bob.delay to get a better view of what is going on. Also, try increasing or decreasing the value of the parameter n. Where is the turtle when the draw function finishes? Draw a stack diagram that shows the state of this program at one of the instants when n==0. Two ways to think about functions --------------------------------- 1) the mechanical view of a function a) locate the appropriate function object b) evaluate the arguments (which are expressions) c) create a frame for the function d) assign the values of the arguments to the parameters e) execute the body of the function f) replace the original function call with the return value g) pick up where you left off 2) abstract view of a function assume that the function works according to its interface (inputs, preconditions, side-effects, postconditions, return value) DON'T think about _how_ it works DON'T follow the flow of execution Example: read tree abstractly def tree(t, dist, n): """draw a tree with n levels of recursion. postcondition: the turtle ends up back where it started. """ if n == 0: return fd(t, dist*n) lt(t, 45) tree(t, dist, n-1) rt(t, 90) tree(t, dist, n-1) lt(t, 45) bk(t, dist*n) Can we prove that turtle ends up back where it started? Fruitful functions ------------------ The functions we have written so far either print something or manipulate turtles, but they don't have return values. Many of the built-in functions we have seen don't print or do anything, but they do return a value: x = math.sin(angle) y = len('allen') You, too, can write fruitful functions. All you need is THE RETURN STATEMENT! def foo(): return 7 x = foo() print x Or, slightly closer to being useful, something like def abs(x): if x<0: return -x elif x>0: return x print abs(3) print abs(-3) print abs(0) Rules of return --------------- 1) a return statement all by itself ends the current function 2) a return statement with a value ends the current function and provides a return value 3) a function that doesn't provide a return value returns None By convention, most functions either 1) always return None (sometimes called void functions) 2) always return a non-None value (called fruitful)