Skip to Content
Course content
Click on the "Edit" button in the top corner of the screen to edit your slide content.

I've noticed a recurring trend when mentoring new developers: they often treat recursion as if it's just a "fancy loop." You'll hear things like, "Why would I use recursion when I could just use a while loop?" This is a dangerous simplification. The misconception is that recursion is just a different syntax for iteration, and that the only thing changing is the loop counter.

To show you why that's wrong, let's look at a piece of code that a loop simply cannot replicate without an explicit, manually managed stack data structure. Look at this "countdown" function:

void count_and_back(int n) {
    if (n <= 0) return;
    printf("Descending: %d\n", n);
    count_and_back(n - 1);
    printf("Ascending: %d\n", n);
}

If you think of this as a loop, you'd expect it to just print "Descending" 3, 2, 1 and then stop. But if you run this with count_and_back(3), you get this:

  • Descending: 3
  • Descending: 2
  • Descending: 1
  • Ascending: 1
  • Ascending: 2
  • Ascending: 3

A loop can go down, but it can't "remember" where it was to come back up in reverse order. This happens because recursion isn't just about repeating a task; it's about stacking contexts.

The Myth: Recursion is just a loop with more steps

The mistake is assuming that the function is simply jumping back to the start. In reality, every time count_and_back calls itself, C creates a brand new stack frame. This frame stores the current value of n and the exact line of code where the function paused.

When we call count_and_back(3), the program doesn't replace the first call; it piles a second call (for n=2) on top of it. Then it piles a third (for n=1) on top of that. The "Ascending" print statements don't even start executing until the base case (n <= 0) is hit and the stack begins to "unwind." It's like a set of Russian nesting dolls: you have to open them all the way to the center before you can start closing them back up.

The Reality: The Call Stack is your hidden memory

This "hidden memory" is what makes recursion powerful. You aren't just iterating; you're postponing work. In the example above, the printf("Ascending...") line is postponed until the recursive call beneath it finishes.

I'll be honest with you: recursion can be a memory hog. Since every call takes up space on the stack, you can't recurse infinitely. If you forget your base case or the recursion depth is too great, you'll hit a Stack Overflow. This isn't a website; it's a literal overflow of the memory region reserved for function calls. The CPU simply runs out of room to push new frames, and your program crashes instantly. Always, always ensure your base case is reachable and handled first.

When deciding between a loop and recursion, ask yourself: "Do I need to remember the state of where I was after the nested operation completes?" If the answer is yes—like when traversing a file directory tree or parsing JSON—recursion is usually the cleaner, more elegant choice. If you're just summing numbers in an array, stick to a for loop. Don't use a sledgehammer to crack a nut.




📋 Practical Task

Implement the Euclidean Algorithm for GCD

One of the most elegant uses of recursion in C is the Euclidean Algorithm for finding the Greatest Common Divisor (GCD) of two integers. The logic is simple: the GCD of two numbers also divides their difference, and more efficiently, the remainder of their division.

Your Task: Write a recursive function int find_gcd(int a, int b) that implements this logic.

Requirements:

  • The base case occurs when b becomes 0; at that point, a is the GCD.
  • The recursive step should call find_gcd passing b as the first argument and a % b (the remainder) as the second.
  • Create a main function to test your implementation with pairs like (48, 18) which should return 6, and (101, 10) which should return 1.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.