0-10255439827_20211018_213137_0000.pdf
Google search result suggestions may show that "Recursion is bad" or "Recursion is magic" but the actual reason why many programmers have problem with recursion is because the call stack is invisible. Recursion should be used either while navigating through tree-like structures or when the problem requires backtracking.
A stack is a data structure that holds a sequence of data and only lets you interact with the top most item. A recursive function call pushes a frame object into stack and returning the method pops them off.
A stack overflow is when a recursive function gets out of control and doesn't stop. Recursion programs can be implemented with loop and stacks.
Recursion is usually slower than iteration due to the overhead of maintaining the stack. Recursion uses more memory than iteration.
Recursion has very high (generally exponential) time complexity whereas loops have relatively lower time complexity (generally polynomial-logarithmic).
A stack is a data structure that holds a sequence of data and only lets you interact with the top most item. A recursive function call pushes a frame object into stack and returning the method pops them off.
A stack overflow is when a recursive function gets out of control and doesn't stop. Recursion programs can be implemented with loop and stacks.
Recursion is usually slower than iteration due to the overhead of maintaining the stack. Recursion uses more memory than iteration.
Recursion has very high (generally exponential) time complexity whereas loops have relatively lower time complexity (generally polynomial-logarithmic).