# Lecture17 - Recursion ECE 3090 Software for Engineering Systems Dr Ayanna Howard School of Electrical and Computer Engineering Georgia Institute of

This preview shows pages 1–8. Sign up to view the full content.

ECE 3090 Software for Engineering Systems Dr. Ayanna Howard School of Electrical and Computer Engineering Georgia Institute of Technology Recursion

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

View Full Document
2 Adapted from “Brief Version of Starting Out with C++” Introduction to Recursion A recursive function contains a call to itself: void countDown(int num) { if (num == 0) cout << "Blastoff!"; else { cout << num << ". ..\n"; countDown(num-1); // recursive } // call } 1. countDown(2) generates the output 2... , then it calls countDown(1) 2. countDown(1) generates the output 1... , then it calls countDown(0) 3. countDown(0) prints Blastoff! , then returns to countDown(1) 4. countDown(1) returns to countDown(2) 5. countDown(2) returns to the calling function
3 Adapted from “Brief Version of Starting Out with C++” Recursive Functions - Purpose Recursive functions are used to reduce a complex problem to a simpler-to-solve problem. The simpler-to-solve problem is known as the base case Recursive calls stop when the base case is reached

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

View Full Document
4 Adapted from “Brief Version of Starting Out with C++” Stopping the Recursion A recursive function must always include a test to determine if another recursive call should be made, or if the recursion should stop with this call In the sample program, the test is: if (num == 0) void countDown(int num) { if (num == 0) // test cout << "Blastoff!"; else { cout << num << ". ..\n"; countDown(num-1); // recursive } // call }
5 Adapted from “Brief Version of Starting Out with C++” Stopping the Recursion Recursion uses a process of breaking a problem down into smaller problems until the problem can be solved In the countDown function, a different value is passed to the function each time it is called Eventually, the parameter reaches the value in the test, and the recursion stops void countDown(int num) { if (num == 0) cout << "Blastoff!"; else { cout << num << ". ..\n"; countDown(num-1); // note that the value } // passed to recursive } // calls decreases by // one for each call

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

View Full Document
6 Adapted from “Brief Version of Starting Out with C++” What Happens When Called? Each time a recursive function is called, a new copy of the function runs, with new instances of parameters and local variables created As each copy finishes executing, it returns to the copy of the function that called it When the initial copy finishes executing, it returns to the part of the program that made the initial call to the function
7 Adapted from “Brief Version of Starting Out with C++” What Happens When Called? third call to countDown num is 0 countDown(1); countDown(0); // no // recursive // call second call to countDown num is 1 first call to countDown num is 2 output: 2...

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 06/09/2009 for the course ECE 3090 taught by Professor Staff during the Fall '08 term at Georgia Institute of Technology.

### Page1 / 91

Lecture17 - Recursion ECE 3090 Software for Engineering Systems Dr Ayanna Howard School of Electrical and Computer Engineering Georgia Institute of

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

View Full Document
Ask a homework question - tutors are online