1

If, for example, I have a small function:

int sum(int a, int b)
{
   int result = a+b;
   return result;
}

Here, the result is a local variable which, from what I understand, should live only for the execution time of the function. How is the caller of this function able to retrieve the return value of the sum() function, which is nothing but the value of the local variable result? Just wanted to know how values of local variables declared inside a function are returned back to the caller functions. I know this happens in the stack but I wanted to know how exactly how it happens.

Peter David Carter
  • 2,548
  • 8
  • 25
  • 44
Akay
  • 1,092
  • 12
  • 32
  • You are confusing the r-value and l-value of a variable. The l-value becomes invalid for you to tamper with after the function returns. The r-value is returned. This means that `return ∑` is invalid but `return sum` isn't. – Spikatrix Mar 20 '16 at 12:30
  • 4
    "I know all this happens in the stack" - on most architectures these days, the return value will be put into a register, not on the stack. With a function as simple as your example, probably all the values will be in registers, and the stack won't be used at all. – Crowman Mar 20 '16 at 12:41

3 Answers3

7

return result; doesn't return the result variable, it returns the value of the result variable. So it's fine that result goes away when the function returns, all we need is its value.

Return values are frequently put in registers rather than the stack, but may be pushed on the stack in some architectures. This page on the cdecl call style gives a good overview. Note that the details vary by calling convention and platform.

But the key point is still the first paragraph: It's the value of result, not result, that the caller receives.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
3

You pass variables values, not variables. In this way you can pass value directly e.g. sum(8).

Everything is implementation specific, but you can assume that calling result = sum(myValueOne, myValueTwo); function looks in this way (of course it's a lie :) ):

  1. Copy myValueOne value to register
  2. Copy myValueTwo value to other register
  3. Jump to function
  4. Execute function using values passed in registers.
  5. Copy result to register
  6. Back to place where function was called
  7. Copy value from register to result variable

You can see it better on the example below:

void increment(int a)
{
   a++;
}

int main(void)
{
   int a =7;
   increment(a);

   printf("My number is now: %d\n", a);  /* Will print 7 */

   return 0;
}

And you are right about variable scope. Here you are the illustration of the wrong function:

int* increment(int a)
{
   int b;
   b = a+1;

   return &b;
}

It returns pointer to (address of) local variable and of course it's wrong, because this variable doesn't exist after function exit. Anyway compiler shall return at least warning in this case.

Cheers, Mikolaj

Mikolaj
  • 138
  • 1
  • 7
1

There is a question Where in memory are my variables stored in c? which says about how things are stored in the computer so local variables is stored in the stack and globals is stored in data

Community
  • 1
  • 1