CMPT 120 Lecture 16 Unit 3 Graphics and





















- Slides: 21
CMPT 120 Lecture 16 – Unit 3 – Graphics and Animation Python – More Turtle, Functions and Introducing Recursion
How about this! • Problem statement: • Write a program that draws a chocolate chip cookie with our Turtle 2
But what if I want many cookies? • Solution 1 • Could I use a for loop? 3
But what if I want many cookies? • Solution 2: • Could I copy and modify the code many times, each instance drawing one cookie? • Hum… This solution would lead to a lot of repeating code, which is not a good idea! Why is that? • See GPS! 4
But what if I want many cookies? • Solution 3: • Best solution would be to use a _________ 5
But what if I want many cookies? • But our function draws many cookies in the same spot! • We have not solve our problem! • Solution 4: • So, the ultimate solution would be to design our function such that it takes _________ ! 6
Function - Local Variables result is a local, temporary variable. It is destroyed once the function call is complete. • Local variables cannot be used outside of their “scope”, i. e. , the function in which they are defined • Parameters are also local variables 7
Why creating functions? Functions make our program easier to … 1. Implement and test -> Incremental development • Dividing a long program into functions allows us to implement, test and debug the parts one at a time and then assemble them into a working program 2. Read • Encapsulate code fragment that has one specific task in one location, i. e. , a “module” (function) and give this location a descriptive name 3. Modify • If we need to make a change to our program, we know “where to go” to find the code fragment we need to change 4. Reuse • Once we write, test and debug a function, we can reuse it in other programs that need this particular functionality 5. No more repeated code • Functions can make a program smaller by eliminating repeated code - Repeated code is very error-prone 8
Turtle Animation 9 https: //repl. it/repls/Avaricious. Weepy. Filename
Recursion Functions that call themselves
Recursion in the real world • Russian dolls 11
Recursion in the real world • Searching for the word “guffaw” in a dictionary 12 Source: http: //www. eslstation. net/ESL 310 L/310 L_dict. htm
Recursion in the real world • Droste Effect The picture is defined by the picture itself. 13
Recursion in the mathematical world 1. Multiply two numbers 2. Compute factorials 3. Fractals The Sierpinski triangle A confined recursion of triangles that form a fractal https: //en. wikipedia. org/wiki/Recursion 14
Recursion - Definition • Recursion occurs when an object or a process is defined in terms of itself (or a version of itself) • In mathematics and computer science, a kind of objects or processes exhibit recursive behavior when they can be defined by two properties: 1. A simple base case (or cases)—a terminating scenario that does not use recursion to produce an answer 2. A set of rules that reduce all other cases toward the base case 15 Adapted from http: //en. wikipedia. org/wiki/Recursion
Recursion in the software world • So far, when solving problems, we have achieved repetition (i. e. , repeating statements in our code) by using iterative statements -> loops • By putting statements we wanted to repeat in a loop 16
Recursive functions • Another way of achieving repetition (i. e. , repeating statements in our code) is by putting statements we want to repeat in a function and calling the function itself a certain number of times function. A • Directly: # recursive call function. A(…) • Indirectly: function 1 # recursive call function 2(…) function 2 # recursive call function 1(…) 17
Let’s give it a try! • Problem statement: • Let’s draw a tree recursively using our turtle 18
Turtle Examples Here are some resources • https: //michael 0 x 2 a. com/blog/turtle-examples (squares) • https: //trinket. io/python/82 fe 4 d 3 bd 0 (interactive) • https: //www. turtle. ox. ac. uk/downloads/docs/Turtle_P ython_Exercises_1 -12. pdf • http: //openbookproject. net/thinkcs/python/english 3 e/ recursion. html • http: //www. 101 computing. net/astronomy-challenge/ 19
Review If you want an action to repeat itself until a certain condition is met 1. When to choose a while loop? When to choose a for loop? I choose a for loop when I know how many times I need to iterate 2. What does it mean when variables in functions have local scope? 20
Next Lecture • We shall practice designing and implementing functions – all sorts of functions • So, bring a laptop!!! 21