lecture4 - * * * * Pass by reference Tail recursion on...

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

View Full Document Right Arrow Icon
* Pass by reference * Tail recursion on lists * Function pointers * Testing intro ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Pass by reference: Recall how functions work: Evaluate the actuals Copy the "values" to the formals Execute the function. This is called "pass-by-value" semantics. With pass-by-value semantics, a function cannot modify the values of any variables passed as arguments (because it operates on a copy of those values). Sometimes this isn't what you want. For any type that is passed by value, we can specify that it should argument list. When a function is called with a pass-by-reference argument, its formal argument is an "alias" (another name) for a variable in the caller's scope. // MODIFIES: x // EFFECTS: increments x. { x = x + 1; } reference. When this function is called from elsewhere in the program: int main() { int i = 0; increment (i); cout << i << endl; } our environment model looks like the following main: -------------- | ------ |
Background image of page 1

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

View Full DocumentRight Arrow Icon
| i: | 0 | | | ------ | -------------- increment: -------------- | | | x: main:i | | | -------------- When we execute x = x + 1, the value of x is changed, but since this happens to be another name for i, we get: main: -------------- | ------ | | i: | 1 | | | ------ | -------------- increment: -------------- | | | x: main:i | | | -------------- And, after increment returns, we destroy the activation record, leaving main: -------------- | ------ | | i: | 1 | | | ------ | -------------- Which is exactly what we were shooting for! Finally, it is sometimes desirable to pass arguments by reference even if they aren't going to be MODIFIED, because copying them is expensive. We will see examples of this later in the course. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ In project two, you're asked to write a function to add all the elements in a list, and another to multiply all the elements in a list. These are almost exactly the same function. Writing almost the exact same function twice is almost certainly a bad idea. There are two main reasons for this: 1) it's wasteful of your time 2) if you find a better way to perform the underlying computation, you have to change many different versions of the same thing; this is prone to error.
Background image of page 2
Remember back in the second lecture, when we defined the notion of a variable's "type": A variable's type tells us two things: * the set of possible values that object may assume * the set of operations possible on/with that object So far, we've been working with types like "int". The type int can take values from the range [INT_MIN . . INT_MAX], and there are a variety of operations (+, -, *, /, etc.) that can be performed on ints. For this class, we'll be talking about the type "list_t".
Background image of page 3

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

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

This note was uploaded on 04/04/2008 for the course EECS 215 taught by Professor Phillips during the Winter '08 term at University of Michigan.

Page1 / 9

lecture4 - * * * * Pass by reference Tail recursion on...

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

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