Ex : Function Implementation:
facto(x){
if(x==1){
return 1;
}
else{
return x*facto(x-1);
}
in more simple way lets take a stack -->
returns
|2(1)|----> 2(1) evaluates to 2
|3(2)|----> 3(2)<______________| evaluates to 6
|4(3)|----> 4(6)<______________| evaluates to 24
|5(4)|----> 5*(24)<____________| evaluates to 120
------ finally back to main...
when a function returns in reverse manner it never knows what exactly is behind it? The stack have activation records stored inside it but how they know about each other who is popped and who is on top? How the stack keeps track of all variables within the function being executed? Besides this, how it keeps track of what code is executed (stackpointer)? When returning from a function call the result of that function will be filled in a placeholder. By using the stackpointer but how it knows where to continue executing code? These are the basics of how the stack works I know but for recursion I don't understand how it exactly works??