im uderstanding the stack in C lenguage, but I have a question; please look at this simple code:
#include <stdio.h>
void function(int x){
if(x==0){
return;
}
else{
printf("x is equal to 1");
}
return function(x-1);
}
void main(){
function(1);
return;
}
So, first the stack frame (or activation record) of main
function its stored on the stack, when we call function(1)
the stack frame of function(1)
gets stored on the stack too; in the function(1)
we enter the else statement and we encounter return function(0);
, so my question is, what happens here?, the stack frame of function(1)
gets popped from the stack because of the return statement and the stack frame of function(0)
gets stored in the stack? or the stack still has the stack frames of main
and function(1)
and the stack frame of function(0)
gets stored as well (having now 3 stack frames in the stack)?
If the second possibility is the right one, so in function(0)
we enter the if statement and because of return, function(0)
stack frame gets popped from the stack, and then function(1)
gets popped and finally main
gets popped from the stack?