What is a recursive function? Provide an example.
Show answer and explanation
A recursive function invokes itself directly or indirectly on a smaller subproblem. It needs a reachable base case and progress toward that case; otherwise it recurses indefinitely, and even correct deep recursion can exhaust the call stack. C++ does not guarantee tail-call optimization, so an iterative algorithm or explicit stack is safer when depth can be large or controlled by input.
A recursive function invokes itself directly or indirectly on a smaller subproblem. It needs a reachable base case and progress toward that case; otherwise it recurses indefinitely, and even correct deep recursion can exhaust the call stack. C++ does not guarantee tail-call optimization, so an iterative algorithm or explicit stack is safer when depth can be large or controlled by input.
Connect the conclusion directly to the type, lifetime, ownership, overload-resolution rule, or library contract present in the question. State implementation dependence or a violated precondition explicitly instead of merely guessing output.