Recursion
A method that calls itself is known as a recursive method.
And, this process is known as recursion.
Example
Writing a Recursive Function?
• Base Case is a small problem that we know how to solve and
is the case that causes the recursion to end. In other words,
it is the case whose solution is pre-known.
• Recursive case: is the more general case of the problem
we’re trying to solve using recursive function.
Recursion v/s Iteration
Recursion Iteration
Recursion uses selection structure. Iteration uses repetition structure
.
Recursion terminates when a base case is An iteration terminates when the loop condition
recognized. fails.
Recursion is usually slower than iteration due to An iteration does not use the stack so it's faster
the overhead of maintaining the stack. than recursion.
Recursion uses more memory than iteration. Iteration consumes less memory.
Recursion makes the code smaller. Iteration makes the code longer.
All recursive algorithms must obey three important laws:
• A recursive algorithm must have a base case.
• A recursive algorithm must change its state and move toward the
base case.
• A recursive algorithm must call itself, recursively.