Chapter 09

Chapter 09 - Chapter 9 Recursion This chapter introduces a...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 9 Recursion This chapter introduces a fundamental new method of programming called “recursion”. Recursion is a powerful idea that makes it possible to solve some problems easily that would otherwise be quite difficult. Recursion cannot be used in all programming languages, but it is supported by most modern languages, including Matlab. 9.1 Activation records In Chapter 7, we learned that Matlab assigns a unique memory location for each variable and that it maintains separate sets of variables for the command window and for each function that is called during the running of a program. The local variables within the scope of one function are distinct from those within the scope of another function and from those of the command window. Two such variables can have the same name, as long as each belongs to a different scope. In this chapter we will learn that Matlab accomplishes this feat by maintaining one area of the computer memory to hold variables used in the command-window and a separate area for each function. The term used in computer science for one such memory area is activation record . An activation record is simply an area of memory in which all information is stored that is needed during the execution of a function or during the execution of a command in the command window. In the design of some early languages, including, notably FORTRAN, which is still a popular language today (and was, incidentally, the language in which the original version of Matlab was written), each activation record was set aside for a specific function before the program ran and was available for the exclusive use of that function whenever it was called. As you will see, Matlab uses a different scheme for allocating areas of memory to its functions, but for now, we will use that model. Suppose we define three Matlab functions, tax , high , and low , as shown in Error: Reference source not found. Suppose we then call tax as follows in the command window: >> wages = 5000; >> due = tax(wages) due = 625 The memory allocation might look something like that shown in Figure 1. The first column of Figure 1 gives the name of the function whose activation record, abbreviated AR ”, is shown. The allocation of memory at the bottom (in light blue) is for the command window, in which two variables are defined— small and due . Since there is always a command window and it always has an activation record to hold the variables
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
defined in it, we have not labeled it. Stacked on top of the memory allocated for the command window is the activation record allocated for the function tax . It contains space for the local variables, income and y ; the activation record for the function high is next. Finally, at the top is the activation record for low .
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/08/2008 for the course CS 103 taught by Professor Fitzpatrick during the Spring '07 term at Vanderbilt.

Page1 / 21

Chapter 09 - Chapter 9 Recursion This chapter introduces a...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online