This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 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 countcontrolled or eventcontrolled. 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 Lets look at an iterative and recursive solution of finding...
View
Full
Document
This note was uploaded on 01/17/2012 for the course CSC 1254 taught by Professor Blanks,l during the Fall '08 term at LSU.
 Fall '08
 Blanks,L
 Recursion

Click to edit the document details