logo
Course thumb

The C programming language made simple

Intro to recursive functions in C

#include <string.h> #include <stdlib.h> #include <stdio.h> // 4 => 4 + 3 + 2 + 1 = 10 // 4 => 4 + 2 + 0 = 6 // 156 => 1 + 5 + 6 = 12 int f(int x) { if (x == 0) { return 0; } printf("Hello from f with x = %d\n", x); return x + f(x - 1); } // return 4 + 3 + 2 + 1 + 0 int main(int argc, char* argv[]) { int result = f(4); printf("The result of calling f is %d\n", result); return 0; }