Recursion
A recursive function needs a base case that stops the calls. Without it the call stack overflows and the program crashes.
Example
#include <stdio.h>
long factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
int main(void) {
printf("%ld\n", factorial(10));
return 0;
}Result
3628800
Exercise
Name the part that stops the calls.
if (n <= 1) return 1; /* case */
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.