Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMPT 120 Lecture 16 – Unit 3 – Graphics and Animation

Similar presentations


Presentation on theme: "CMPT 120 Lecture 16 – Unit 3 – Graphics and Animation"— Presentation transcript:

1 CMPT 120 Lecture 16 – Unit 3 – Graphics and Animation
Python – More Turtle, Functions and Introducing Recursion

2 How about this! Problem statement:
Write a program that draws a chocolate chip cookie with our Turtle

3 But what if I want many cookies?
Solution 1 Could I use a for loop?

4 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?

5 But what if I want many cookies?
Solution 3: Best solution would be to use a __________________

6 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 __________________ !

7 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

8 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

9 Turtle Animation

10 Functions that call themselves
Recursion Functions that call themselves

11 Recursion in the real world
Russian dolls

12 Recursion in the real world
Searching for the word “guffaw” in a dictionary Source:

13 Recursion in the real world
Droste Effect The picture is defined by the picture itself.

14 Recursion in the mathematical world
Multiply two numbers Compute factorials Fractals The Sierpinski triangle A confined recursion of triangles that form a fractal

15 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

16 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

17 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(…)

18 Let’s give it a try! Problem statement:
Let’s draw a tree recursively using our turtle

19 Turtle Examples Here are some resources
(squares) (interactive)

20 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

21 Next Lecture We shall practice designing and implementing functions – all sorts of functions So, bring a laptop!!!


Download ppt "CMPT 120 Lecture 16 – Unit 3 – Graphics and Animation"

Similar presentations


Ads by Google