Recursion is the method which calls it self. It can be applicable where the solution of a problem is depended on the smaller instances of the same program. For example assume that we need to find the nth factorial,
- n! = n* (n-1) * (n-2)*…………………..*3*2*1
- Here, the
- n! = n * (n-1)!
- (n-1)! = (n-1) * (n-2)!
- (n-2)! = (n-2) * (n-3)!
- (n-3)! = (n-3) * (n-4)!
- (n-4)! = (n-4) * (n-5)!
- (n-5)! = (n-5) * (n-6)! And so on.
- So when we find the solution for the smaller instance it will help to find the solution for the corresponding nth
Like this there are many problems which are depended on their smaller instances. So recursion methods can be helpful in such cases
Advantages of recursion method
- Makes algorithms more elegant
- Reduces the amount of codes or procedures
- Can solve problems easily while the iteration method is more complex
Disadvantages of recursion method
- It is hard to trace the recursion idea since it is logical
- Always need a termination control
- It consumes lots of stack space, not preferable when the program size is small
- Need more processing time.
University of Jaffna