07-26-2023, 07:13 AM
Writing recursive functions without a proper base case can lead to infinite recursion.
// Incorrect
void countDown(int n) {
printf("%d ", n);
countDown(n - 1); // Missing base case
}
// Correct
void countDown(int n) {
if (n <= 0) {
return;
}
printf("%d ", n);
countDown(n - 1);
}
// Incorrect
void countDown(int n) {
printf("%d ", n);
countDown(n - 1); // Missing base case
}
// Correct
void countDown(int n) {
if (n <= 0) {
return;
}
printf("%d ", n);
countDown(n - 1);
}