In: Computer Science
1.what is a base condition of recursive function?
2. why is the base condition of recursive function important?
Here is an example first and answers later:
int factorial(int n){
if (n==1){
return 1;
}
return n*factorial(n-1);
}
The above function is a recursive function because the function is calling it self.
Because the function is calling itself there should be a condition where it needs to stop calling itself.
1) A base condition is a recurrence breaking condition of a recursive function
example: in the above factorial program the base condition is when n=1 we are stopping the recursive call and returning 1.
2) If a base condition was not set properly the function will not stop calling itself forever and stack memory will fill and the program will crash. So we should specify a base condition properly.