{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

csc1254lect06

csc1254lect06 - CSc 1254 Lecture 6 Recursion Looping...

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

CSc 1254: Lecture # 6 Recursion September 26, 2005 Looping: Iteration v. Recursion Writing recursive functions Pitfalls of recursion The Box Method 1 Looping Iterative constructs are looping constructs such as while and for that are count-controlled or event-controlled. On the other hand a recursive function is one that calls itself and runs until a base case is reached. A base case is a special case whose solution we already know. Recursion can seem unreal at first, but as you will see, recursion is a very real and important problem solving approach that is an alternative to iteration. Some recursive solutions are impractical and inefficient. But some complex problems can have simple and elegant recursive solutions. Now, we consider the recursive and iterative definitions of a simple function – the power function. Iterative definition: x n = n producttext i =1 x = x * x * x * ... * x bracehtipupleft bracehtipdownrightbracehtipdownleft bracehtipupright n times Recursive definition: x n = braceleftbigg n = 0 , 1 else x * x n - 1 1

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.

{[ snackBarMessage ]}