C-18
-
Upload
rushdi-shams -
Category
Education
-
view
72 -
download
3
description
Transcript of C-18
Lecture 18Lecture 18Version 1.0Version 1.0
RecursionRecursion
2Rushdi Shams, Dept of CSE, KUET, Bangladesh
What is RecursionWhat is Recursion
A function is called ‘recursive’ if a A function is called ‘recursive’ if a statement within the body of a statement within the body of a function calls the same function function calls the same function
Sometimes called ‘circular Sometimes called ‘circular definition’ definition’
3Rushdi Shams, Dept of CSE, KUET, Bangladesh
IterationIteration
4Rushdi Shams, Dept of CSE, KUET, Bangladesh
RecursionRecursion
5Rushdi Shams, Dept of CSE, KUET, Bangladesh
ExplanationExplanation
6Rushdi Shams, Dept of CSE, KUET, Bangladesh
More Simpler RecursionMore Simpler Recursion
7Rushdi Shams, Dept of CSE, KUET, Bangladesh
And the classical And the classical problemproblem
8Rushdi Shams, Dept of CSE, KUET, Bangladesh
Iteration vs RecursionIteration vs Recursion 1. Both iteration and recursion are based on 1. Both iteration and recursion are based on
control structure. Iteration uses a repitation control structure. Iteration uses a repitation structure and recursion uses a selection structure and recursion uses a selection structure.structure.
2. Both involves in a termination state checking. 2. Both involves in a termination state checking. 3. Both can be run infinitely. (which is of course 3. Both can be run infinitely. (which is of course
not desirable)not desirable) 4. Recursion is expensive in case of memory and 4. Recursion is expensive in case of memory and
processing time than iteration.processing time than iteration. 5. Recursion for many cases makes programs 5. Recursion for many cases makes programs
easier.easier. 6. Recursion is the style of stylish programmer. 6. Recursion is the style of stylish programmer.