{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture17

# Lecture17 - Recursion ECE 3090 Software for Engineering...

This preview shows pages 1–9. 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... 1... Blastoff! return return return

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

View Full Document
8 Adapted from “Brief Version of Starting Out with C++” The Recursive Factorial Function The factorial function: n! = n*(n-1)*(n-2)*...*3*2*1 if n > 0 n! = 1 if n = 0 Can compute factorial of n if the factorial of (n-1) is known: n! = n * (n-1)!
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern