Software Design Spring 2008 For today, you should have: 1) read Chapters 4 and 6 and prepared for a quiz 2) worked on Homework 2 For lab: 1) read Chapter 7 of "How to think..." 2) finish Homework 2, check it into your repository AND bring a hardcopy for f2f evaluation. For Thursday: 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. Koch ---- The Koch curve is a fractal that looks something like the picture on page 55 of the book. To draw a Koch curve with length x, all you have to do is 1) Draw a Koch curve with length x/3. 2) Turn left 60 degrees. 3) Draw a Koch curve with length x/3. 4) Turn right 120 degrees. 5) Draw a Koch curve with length x/3. 6) Turn left 60 degrees. 7) Draw a Koch curve with length x/3. The only exception is if x is less than 2. In that case, you can just draw a straight line with length x. Write a function called koch that takes a turtle and a length as parameters, and that uses the turtle to draw a Koch curve with the given length. Then write a function called snowflake that draws three Koch curves to make the outline of a snowflake. 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)