Download presentation
Presentation is loading. Please wait.
Published byOliver Graversen Modified over 5 years ago
1
CMPT 120 Lecture 19 – Unit 3 – Graphics and Animation
Python – Implementing and Visualizing Recursion
2
Review – What to consider when creating a function
Function interface Function name (to reflect the task performed by the function) Parameter(s) Returned value
3
Review – What to consider when creating a recursive function
Base Case Stops the function from calling itself indefinitely Recursive Case This is where the function calls itself with a modified (often smaller) form of the problem How do we get this “modified form of the problem” -> a rule
4
Last Lecture – we were here!
Descriptive function name 1 parameter -> n Docstring describes the task performed by our function Let’s complete our program!
5
Execution Flow and Recursion
How does recursion actually work? How can we test our recursive function? Let’s have a look at it through the Python Code Visualizer Online textbook calls this box tracing Box tracing technique used to hand trace the execution of a function (especially a recursive function) in order to figure out what it does
6
Box tracing a recursive function
Knowing how to hand/box trace code is very important Even more important when dealing with recursive functions Why? Because following the execution of recursive functions can become very complicated very quickly Box tracing allows us to figure out what a recursive function does (keeping track of its parameters and local variables), what it produces (keeping track of its returned value) and where the execution flow returns to in a systematic way
7
Let’s try this again! Problem Statement
Write a Python function that reverse a string
8
Let’s go back to our Turtle Trees!
And have a look at more complicated recursive function that draws trees using a dictionary What does a dictionary have to do with trees?
9
Next Lecture Feedback on our algorithms of last Friday
New practice exercises
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.