02-recursion.student - Last Time Manifesto Spend less time...

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

View Full Document Right Arrow Icon
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
Image of page 1

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

View Full Document Right Arrow Icon
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 | +--------------------+ plus_one: +--------------------+ | x: 0 | +--------------------+
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern