week2notes - EECS 280 Discussion Notes - Week 2 Recursion...

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: EECS 280 Discussion Notes - Week 2 Recursion and GDB Recursion is the first major topic in computer science that many students have trouble with (the other two being pointers and dynamic memory management, which well get to later in the course). In this discussion, well see that recursive functions are just functions that call themselves. 1 The Call Stack First, some background: how do you describe where the computer is as its executing a program? Consider the following: #include < iostream > using namespace std ; void print ( string word) { cout << word << endl ; cout << I printed something again ! << endl ; } void print today () { string date = Jan 15 2008; print ( date ) ; } void pr i nt he l l o () { print ( Hi ! ) ; } int main () { print today ( ) ; pr i nt he l l o ( ) ; pr i nt he l l o ( ) ; } What if I say that the computer is on the second line of print()? Well, that by itself isnt enough information. This program, when run, calls print() three times, so according to my description, we could be at any of three places in the program: called by print today() or by the first or second call to print hello(). Therefore, the current line in the program isnt enough; we also need to know which function called the current function (and on what line), all the way back to main(). A complete description might be: line 2 of print(), called from 1 line 1 of print_hello(), called from line 3 of main(). In reality, the computer maintains all this information using a stack of activation records containing the local variables in each function, which well see in the next example. 2 Recursion Suppose I want to multiply two integers, x and y, storing the result into z, but without using the multiplication (*) operator. (Theoretical computer scientists think this sort of thing is clever.) How can I do this? Well, x times y is x added to itself y times, so we can add x y times into z (initially zero). So, in C++: // Multiplies two numbers via addition // REQUIRES: y > = 0 // EFFECTS: returns x * y int multiply ( int x , int y) { int z = 0; while (y > 0) { z += x ; y- = 1; } return z ; } Does this work? Yes. Does it work if x = 0? y = 0? Yes and yes. What if x < 0? Yes. If y < 0? No. (Hence the REQUIRES clause.) However, there is another way to define multiply: multiply_r(x,y) = if y == 0 = x + multiply_r(x,y-1) if y > 0 This is the recursive definition of multiply, which we call multiply r. Recursion is the process of rerunning a function somewhere inside itself. The base case (the stopping condition) is when y == 0; multiplya function somewhere inside itself....
View Full Document

Page1 / 7

week2notes - EECS 280 Discussion Notes - Week 2 Recursion...

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