Recursion Flashcards
What is recursion in programming?
Recursion is a programming technique where a method (or function) calls itself to solve a problem.
Why might recursion seem like a strange or risky approach at first?
It may seem strange or risky because it involves a method calling itself, which could potentially lead to an infinite loop and crash the program if not handled correctly.
What are the three key features common to all recursive routines?
A recursive routine:
Calls itself.
Solves a smaller problem with each recursive call.
Has a base case that stops the recursion.
What are the potential drawbacks of using recursion compared to iteration (using loops)?
Recursion can be less efficient due to the overhead of multiple method calls and the use of stack memory, which could lead to stack overflow in cases of deep recursion.
Why might recursion be considered conceptually simpler for some problems despite being less efficient?
Recursion can simplify the understanding and implementation of problems that naturally fit a divide-and-conquer approach, such as tree traversals, even if it introduces some performance overhead.