Middle
What cases must necessarily be present in a recursive function?
sobes.tech AI
Answer from AI
Necessary components of a recursive function:
- Base Case: The condition under which recursion stops. Without it, the function will call itself infinitely, leading to stack overflow.
- Recursive Step: Calling the function itself with modified arguments that gradually approach the base case.
Example:
def factorial(n):
# Base case
if n == 0:
return 1
# Recursive step
else:
return n * factorial(n-1)