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);
}
Also follow me on Youtube for videos about video games:
https://www.youtube.com/channel/UCxfkGVU...2mQ/videos
https://www.youtube.com/channel/UCxfkGVU...2mQ/videos