Recursion in C

Recursion is a technique where a function calls itself until a base condition is reached. It is useful for problems that can be broken into smaller sub-problems.

Why Use Recursion?

Factorial Example


#include <stdio.h>

int fact(int n){
  if(n == 1) return 1;
  return n * fact(n-1);
}

int main(){
  printf("%d", fact(5));
}

Expected Output

120

Important Rules