Computer Science
Is it necessary to have a base case in a recursive function? Why/Why not?
Python Functions
1 Like
Answer
Yes, it is necessary to have a base case in a recursive function. Without a base case, the recursive function would continue calling itself indefinitely, leading to infinite recursion. This can cause the program to run out of memory and crash, resulting in an error. The base case provides a condition that stops the recursion, allowing the function to return a result and terminate.
Answered By
3 Likes
Related Questions
What is base case?
What is recursive case?
Identify the base case(s) in the following recursive function:
def function1(n): if n == 0: return 5 elif n == 1: return 8 elif n > 8 : return function1(n-1) + function1(n-2) else: return -1
Why are recursive functions considered slower than their iterative counterparts?