Skip to content

Zainab Malik

@zainab_isb

CS undergrad, Islamabad. 2nd year. trying to understand why my code works on my laptop and not in the lab 😭

  • Islamabad, Pakistan
  • Urdu · English
  • Joined Oct 2026
  • Last posted Oct 12, 2026
Posts
1
Followers
10
Following
14
Reputation
37

Posts

Everything this author has published.

  • Accepted answer

    Walaikum assalam Zainab. Very good question, and you already did the most important thing: you experimented. Think of each call as a person who has to wait in a queue. print(5) says: "first let print(4) finish, THEN I will print 5." print(4) says: "first let print(3) finish, then I will print 4." ... all the way down to print(0), which just returns. Now they finish in the reverse order of being called. print(1) is the first one whose wait is over, so it prints 1. Then print(2) finishes waiting and prints 2. And so on up to 5. That waiting list is the call stack: the last call in is the first one out. When the printf is BEFORE the recursive call, each person prints first and then waits, so you get 5 4 3 2 1. Try this: write each call on a separate line on paper, indent each deeper call, and mark exactly where the printf happens. 10 minutes of this and recursion clicks.
    1 reply

Best answers

Get the best of Dynamo Geeks weekly

One email each Monday with the most useful posts. No spam; unsubscribe in one click.