Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9 Recursion.

Similar presentations


Presentation on theme: "Chapter 9 Recursion."— Presentation transcript:

1 Chapter 9 Recursion

2 Outline 9.1 Concept: The Activation Stack 9.2 Recursion Defined 9.3 Implementing a Recursive Function 9.4 Exceptions 9.5 Wrapper Functions 9.6 Examples of Recursion

3 9.1 Concept: The Activation Stack
We have seen that the Command Window, scripts and functions have their own workspaces where local variable names and values are stored. The mechanism for managing workspaces is the Activation Stack, a collection of stack frames. When the Command Window is opened, an new frame is allocated from the Activation Stack to contain its workspace. When a script is run, it shares the currently active workspace When a function is called, The current workspace is suspended A new frame is fetched from the stack. When the function finishes, it releases its frame back to the stack and re-activates the calling workspace

4 9.2 Recursion Defined Recursion is a different way of creating repetitive execution of a code block. For and while loops use local variables to manage the repetition of a code block. Recursion uses the activation stack to manage recursion by allowing a function to call a clone of itself. The code block being repeated is the body of the function. Definition: like cloning sheep, a clone of a function has exactly the same functionality as the original function, but it is in a different location: a different stack frame.

5 9.3 Implementing a Recursive Function
The obvious question, then, is how to stop the repetition. A recursive function must have three characteristics: At the beginning of the function, there must be a termination test to determine whether the repetition should continue. The function must call a clone of itself The parameters of that call must move the data towards the terminating condition.

6 9.4 Exceptions Most modern computer languages announce serious errors by throwing exceptions which in MATLAB usually results in red ink in the Command Window. When an exception is thrown, all of the frames of the activation stack are discarded until a frame is found that catches that exception. If no frame is found to catch the exception, the stack frame for the Command Window is activated and the error announced. To catch an exception, your code must be executing a try … catch code block (next slide). Some built-in MATLAB functions do this (e.g. input(…))

7 Try … Catch A try … catch block allows the user to identify a code body that might be error-prone and stop any errors from propagating to the Command Window. It must have two code blocks: The suspect code The logic for resolving the problem An example in pseudo-code: good = false while ~good try ask for a file name open the file to read it good = true % only executed if the file exists catch % file does not exist warn the user end

8 9.5 Wrapper Functions Frequently, we find that recursive functions are somewhat fragile – strange input data values cause them to “hang up” – not forever like a while loop, though, because MATLAB limits the number of frames you can have from the Activation Stack. The first function in the file is often a simple function that: tests or sanitizes the data provided by the caller, throws an exception if it is irrecoverable (the error(…) function), or Calls a recursive helper function to solve the problem

9 9.6 Examples of Recursion The classic examples include: factorial:
N! = N * (N-1)! With termination when 0! = 1; The Nth Fibonacci number Fib(N) = fib(N-1) + fib(N-2) terminating when fib(1) = fib(2) = 1 Detecting a palindrome (see code in the book) Mathematical relationships including GCF

10 Let’s write some code …


Download ppt "Chapter 9 Recursion."

Similar presentations


Ads by Google