Ad space (banner)
๐Ÿ”งC Lessons
Lesson 12 / 68

Recursive Functions

This lesson covers writing a recursive function in C, so you understand the mechanism of a function calling itself. It's for anyone searching "C recursive function usage."

A recursive function is a function that calls itself. It's well-suited to logic that repeats the same calculation pattern, like "n! = n ร— (n-1)!".

The example's factorial function calls itself as n * factorial(n - 1), computing the factorial of 5. The base case, if (n <= 1) return 1;, ensures the chain of calls eventually stops.

A common early mistake is forgetting the base case. Without one, the function calls itself forever, causing the fatal error known as a stack overflow.

In real projects, C often favors rewriting recursion as a loop instead, to avoid the overhead of recursive function calls.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run C directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)