Ask HN: How to trace a void recursive function?

1 shivajikobardan 0 9/2/2025, 8:15:24 AM
I was going over [this](https://stackoverflow.com/questions/66712061/how-do-i-trace-a-recursive-function-with-a-return-function-in-it) link. And suddenly I remembered a problem that I faced yesterday. It was related to tracing of a void returning recursive function with a printer.

Eg(pseudocode)

    func(int x)
    {
    if(x>0){
    
    print(before execution:x);
    
    func(x-1);
    
    print(after execution: x);
    }
    }

Call this function with x=5

    Before execution n=5 
    Before execution n=4 
    Before execution n=3 
    Before execution n=2 
    Before execution n=1 
    After execution n=1 
    After execution n=2 
    After execution n=3 
    After execution n=4 
    After execution n=5 
    
The output will appear like this.

Before execution part is not suprising. It is pretty obvious. But after execution part is noteworthy for me specially.

I can naively guess that it is a call stack that is being used. And it is just popping the value from the top of the stack that was pushed earlier. But I am not exactly sure of the architecture of data structure that is being used and methodologies that are being followed at code and hardware level. As a CS enthusiast, it is my basic need to understand this. Hope to get some insights here.

Comments (0)

No comments yet