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
But what if I want many cookies? Solution 1 Could I use a for loop?
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! See GPS! Why is that?
But what if I want many cookies? Solution 3: Best solution would be to use a __________________
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 __________________ !
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
Why creating functions? Functions make our program easier to … 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 Read Encapsulate code fragment that has one specific task in one location, i.e., a “module” (function) and give this location a descriptive name 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 Reuse Once we write, test and debug a function, we can reuse it in other programs that need this particular functionality No more repeated code Functions can make a program smaller by eliminating repeated code - Repeated code is very error-prone
Turtle Animation https://repl.it/repls/AvariciousWeepyFilename
Functions that call themselves Recursion Functions that call themselves
Recursion in the real world Russian dolls
Recursion in the real world Searching for the word “guffaw” in a dictionary Source: http://www.eslstation.net/ESL310L/310L_dict.htm
Recursion in the real world Droste Effect The picture is defined by the picture itself.
Recursion in the mathematical world Multiply two numbers Compute factorials Fractals The Sierpinski triangle A confined recursion of triangles that form a fractal https://en.wikipedia.org/wiki/Recursion
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: A simple base case (or cases)—a terminating scenario that does not use recursion to produce an answer A set of rules that reduce all other cases toward the base case 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
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 Directly: Indirectly: functionA # recursive call functionA(…) function1 # recursive call function2(…) function 2 # recursive call function1(…)
Let’s give it a try! Problem statement: Let’s draw a tree recursively using our turtle
Turtle Examples Here are some resources https://michael0x2a.com/blog/turtle-examples (squares) https://trinket.io/python/82fe4d3bd0 (interactive) https://www.turtle.ox.ac.uk/downloads/docs/Turtle_Python_Exercises_1-12.pdf http://openbookproject.net/thinkcs/python/english3e/recursion.html http://www.101computing.net/astronomy-challenge/
I choose a for loop when I know how many times I need to iterate Review If you want an action to repeat itself until a certain condition is met When to choose a while loop? When to choose a for loop? What does it mean when variables in functions have local scope? I choose a for loop when I know how many times I need to iterate
Next Lecture We shall practice designing and implementing functions – all sorts of functions So, bring a laptop!!!