02-recursion.student - Last Time: * Manifesto: Spend less...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Last Time: * Manifesto: Spend less time writing more correct programs! * Interfaces: the contract between client and implementor * RME clauses: way to specify that contract Outline: * RME Examples * Nature of recursion * Recursive functions * Recursion: a problem Administrative notes: * Make a point of reviewing both text and slide versions of lecture notes. The text notes have more details, but the slide notes tell you what *we* think is important! * Discussion this week: using our environment. ++++++++++++++++++++++++++++++++++++++++++++++++++++++ Before we get to the next topic, we first have to remind you how function call works, and specifically, how the "call stack" works. When we call a function (using pass-by-value semantics) the program follows these steps: 1: Evaluates the actual arguments to the function. (Note, the order is not guaranteed.) 2: Creates an "activation record" (sometimes called a "stack frame") to hold the function's formal arguments and local variables. 3: Copies the actuals' values to the formals' storage space 4: Evaluates the function in its local scope 5: Replaces the function call with the result. 6: Destroys the activation record. Activation records are typically stored as a *stack*. To understand how this works, think about the stack of clean plates in a typical residence hall cafeteria. When a plate is cleaned, it is put on the top of the stack. When someone needs a plate, they take it from the top of the stack. Cleaning a plate is like calling a function; taking a plate is like returning from that function. Activation records work just like this: when a procedure is called, an activation record for *this* invocation is added to the "top" of the stack. When that procedure returns, it's record is removed from the "top" of the stack. In the meantime, this procedure may have called other functions, (creating corresponding activation records), but they all must have returned (destroyting corresponding activation records) before this procedure can return. I put "top" in quotes, because, by convention, stacks are typically drawn growing *down* rather than *up*. This also happens to be the way in which stacks are actually implemented in a process' memory, as you can discover if you look at the address of stack variables in a debugger. For example, consider the following fragment of code: int plus_one(int x) { return (x+1); } int plus_two(int x) { return (1 + plus_one(x)); } int main() { int result = 0; result = plus_two(0); cout << result; return 0; } Main starts out with an activation record with room only for the local "result": main: +--------------------+ | result: 0 | +--------------------+ Then, main calls plus_two, passing the literal value "0": main: +--------------------+ | result: 0 | +--------------------+ plus_two: +--------------------+ | x: 0 | +--------------------+ Which in turn calls plus_one: main: +--------------------+ | result: 0 | +--------------------+ plus_two: +--------------------+ | x: 0 | +--------------------+...
View Full Document

This note was uploaded on 02/14/2012 for the course EECS 280 taught by Professor Noble during the Winter '08 term at University of Michigan.

Page1 / 9

02-recursion.student - Last Time: * Manifesto: Spend less...

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