c-18

8
Lecture 18 Lecture 18 Version 1.0 Version 1.0 Recursion Recursion

Upload: rushdi-shams

Post on 19-Jan-2015

72 views

Category:

Education


3 download

DESCRIPTION

 

TRANSCRIPT

Page 1: C-18

Lecture 18Lecture 18Version 1.0Version 1.0

RecursionRecursion

Page 2: C-18

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’

Page 3: C-18

3Rushdi Shams, Dept of CSE, KUET, Bangladesh

IterationIteration

Page 4: C-18

4Rushdi Shams, Dept of CSE, KUET, Bangladesh

RecursionRecursion

Page 5: C-18

5Rushdi Shams, Dept of CSE, KUET, Bangladesh

ExplanationExplanation

Page 6: C-18

6Rushdi Shams, Dept of CSE, KUET, Bangladesh

More Simpler RecursionMore Simpler Recursion

Page 7: C-18

7Rushdi Shams, Dept of CSE, KUET, Bangladesh

And the classical And the classical problemproblem

Page 8: C-18

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.