70 likes | 174 Views
Recursion. Dilshad M. Shahid New York University @1999. Today. Definition Features of Recursion Recursion Examples Recursion vs Iteration. Definition. A recursive function is a function that calls itself either directly or indirectly through another function
E N D
Recursion Dilshad M. Shahid New York University @1999
Today • Definition • Features of Recursion • Recursion Examples • Recursion vs Iteration
Definition • A recursive function is a function that calls itself either directly or indirectly through another function • A recursive function is called to solve a problem • the function knows only how to solve the simplest case (base case)
Definition • if function is called with a base case, it simply returns the result • if function is called with a more complex problem, it divides the problem into 2 conceptual pieces - a piece it knows how to do and a piece it doesn’t
Features of Recursion • Remember, a recursive function is one that calls itself • The object of a recursive function is to reduce a complex problem to a simple case (base case) that it can solve, and then work forward step by step, solving each step until the solution to the original problem is found • So in order for the recursion to eventually terminate, the sequence of smaller and smaller problems must eventually converge on the base case
Recursion Examples • Factorial example • Fibonacci sequence example • Both on webpage
Recursion vs Iteration • See section 5.15 on page 180 of your text