



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Recursion, a programming concept where functions call themselves to solve complex problems. It uses examples of computing factorials and generating fibonacci series recursively. The factorial function definition, call sequence, and results, as well as the fibonacci function definition and code.
Typology: Slides
1 / 7
This page cannot be seen from the preview
Don't miss anything!




Factorial ( 5 ) if 5 = 1 return 1 else return 5 * Factorial (4) Factorial ( 4 ) if 4 = 1 return 1 else return 4 * Factorial (3) Factorial ( 3 ) if 3 = 1 return 1 else return 3 * Factorial (2) Factorial ( 2 ) if 2 = 1 return 1 else return 2 * Factorial (1) Factorial ( 1 ) if 1 = 1 return 1 else return 5 * Factorial (4) 1 24 6 2
Example Using Recursion: The Fibonacci Series
Fib ( 5 ) if 5 = 1 or 5 = 2 return 5- else return Fib (4) + Fib (3) Fib ( 4 ) if 4 = 1 or 4 = 2 return 4- else return Fib (3) + Fib (2) Fib ( 3 ) if 3 = 1 or 3 = 2 return 3- else return Fib (2) + Fib (1) Fib ( 2 ) if 2 = 1 or 2 = 2 return 2- else return Fib (1) + Fib (0) Fib ( 3 ) if 3 = 1 or 3 = 2 return 3- else return Fib (2) + Fib (1) Fib ( 2 ) if 2 = 1 or 2 = 2 return 2- else return Fib (1) + Fib (0) Fib ( 1 ) if 1 = 1 or 1 = 2 return 1- else return Fib (0) + Fib (-1) Fib ( 1 ) if 1 = 1 or 1 = 2 return 1- else return Fib (0) + Fib (-1) Fib ( 2 ) if 2 = 1 or 2 = 2 return 2- else return Fib (1) + Fib (0) 1 0 1 1 1 0 2 1 3