# Topic5 - Introduction to C+ Recursion Topic #5 CS162 Topic...

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

Introduction to C++ Recursion Topic #5 1 CS162 Topic #5

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

View Full Document
CS162 - Topic #5 • Lecture: Recursion – The Nature of Recursion – Tracing a Recursive Function – Work through Examples of Recursion – Problem solving with recursion 2 CS162 Topic #5
CS162 - Recursion • Recursion is repetition (by self-reference) – it is caused when a function calls/invokes itself. – Such a process will repeat forever unless terminated by some control structure. 3 CS162 Topic #5

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

View Full Document
CS162 - Recursion • So far, we have learned about control structures that allow C++ to iterate a set of statements a number of times. • In addition to iteration, C++ can repeat an action by having a function call itself. – This is called recursion. In some cases it is more suitable than iteration. 4 CS162 Topic #5
CS162 - Recursion • While recursion is very powerful – and will allow us to at times simply solve complex problems – it should not be used if iteration can be used to solve the problem in a maintainable way (i.e., if it isn’t too difficult to solve using iteration) – so, think about the problem. Can loops do the trick instead of recursion? 5 CS162 Topic #5

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

View Full Document
CS162 - Recursion – Why select iteration versus recursion? • Efficiency! • Every time we call a function a stack frame is pushed onto the program stack and a jump is made to the corresponding function • This is done in addition to evaluating a control structure (such as the conditional expression for an if statement) to determine when to stop the recursive calls. • With iteration all we need is to check the control structure (such as the conditional expression for the while, do-while, or for) 6 CS162 Topic #5
CS162 - Recursion • Let's look at a very simple example; – in this case we can see that by using recursion we can make some difficult problems very trivial. .. – many of these problems would be very difficult to solve if you only were able to use iteration. trace through the following problem in class. ..showing how the stack frame works 7 CS162 Topic #5

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

View Full Document
CS162 - Recursion • What is the purpose of the following? void strange(void); int main(){ cout <<"Please enter a string" <<endl; strange(); cout <<endl; return 0; } void strange(void) { char ch; cin.get(ch); strange(); cout <<ch; } } 8 CS162 Topic #5
• This program writes the reverse of what was entered at the keyboard, no matter how many characters were entered! Try to write an equally simple program just using the iterative statements we know about; it would be difficult to make it behave the same without limiting the number of characters that can be entered or using up a lot of memory with a huge array of characters! • Notice, with recursion, we didn't have to even use an array!!

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 09/30/2011 for the course CS 162 taught by Professor Fant during the Spring '08 term at Portland State.

### Page1 / 31

Topic5 - Introduction to C+ Recursion Topic #5 CS162 Topic...

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

View Full Document
Ask a homework question - tutors are online