0

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?

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
Cblue X
  • 143
  • 6
  • 4
    The code shown does not compile (should not compile). You should not attempt to return the value from a function that does not return a value. – Jonathan Leffler Sep 03 '23 at 03:57
  • Other issues aside, the stack is [LIFO](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)). – Ouroborus Sep 03 '23 at 04:46
  • The C standard doesn't describe such things. The C standard doesn't even require (nor mention) things like call stacks. – Support Ukraine Sep 03 '23 at 04:46
  • But it compiles perfectly, no warnings or errors, at least in GBD online – Cblue X Sep 03 '23 at 05:30
  • `void` means that the function does NOT return a value, which means that anything other than `return;` should be a compiler error. You need to understand what a function return value is (and is not) before you go any further. Enable all compiler hints/warnings/errors while you're learning. If the compiler you're using does not give you any of those things on the code you've posted, find a better compiler. – Ken White Sep 03 '23 at 05:37

1 Answers1

2

If your compiler and the settings you compile with support it, tail call optimization1 may occur, and in your case the stack space for function(0) reuses the stack space for function(1).

However, this is not guaranteed in C and should not be counted on.

Without this optimization, all of your stack frames will accrue, and given a sufficiently large initial value passed into function you will get a stack overflow error.

As noted by Jonathan Leffler in comments, you should not return the result of the recursive call, as function is a void function. Further, the else is extraneous given the way control flow is handled.

void function(int x) {
    if (x == 0) {
        return;
    }
    
    printf("x is equal to 1");   
    function(x-1);
}

1 recommended further reading: What is tail call optimization?

Chris
  • 26,361
  • 5
  • 21
  • 42
  • Thanks Chris!, but let me see if i really understand, so, if my compiler has this optimizacion then function(0) stack frame will replace function(1) stack frame right? this according to my exact code right? And if my compiler doesnt not have this optimizacion then the second case i wrote will present right? But now – Cblue X Sep 03 '23 at 04:09
  • So i should not return anything in a void type function right? but i s it ok to write "return;" just for legibility reasons in void functions? is this correct? And well, doing the changes to the function definition you wrote, all you have said applies again for this new code (the two cases depending on the optimization)? – Cblue X Sep 03 '23 at 04:12
  • Yes, a bare `return` for control flow purposes is fine. – Chris Sep 03 '23 at 04:29