Example 1 — Down and up
Print on the way down and again on the way up.
#include <stdio.h>
static void f(int n) {
if (n == 0) {
printf("base case reached\n");
return;
}
printf("down %d\n", n);
f(n - 1);
printf("up %d\n", n);
}
int main(void) {
f(3);
return 0;
} How It Works
- Calls for 3, 2, and 1 print "down" before recursing.
- The base case ends the descent.
- The pending calls then resume and print "up" in reverse order.
