16

Why this two functions return different values?

When I call this function passing 0 as parameter it returns 1

public static int IncrementByOne(int number)
{
    return (number + 1);
}

However, when I call this function passing 0 as parameter it returns 0 even though the increment is executed and the number variable changes its value to 1 inside the method?

public static int IncrementByOne(int number)
{
    return number++;
}

What is the reason why the returned values of this two functions are different?

Esteban Verbel
  • 738
  • 2
  • 20
  • 39

4 Answers4

30

number++ is a postincrement. It returns its current value before it is incremented. To get the same behaviour as in your first method, use a preincrement ++number

See documentation: https://msdn.microsoft.com/en-us/library/36x43w8w.aspx

Flat Eric
  • 7,971
  • 9
  • 36
  • 45
6

The value of the post-increment (postfix) ++ operator is the value of the operand before it is incremented. So if the current value is 2, the operator saves 2, increments it to 3 but returns the saved value.

For your function

public static int IncrementByOne(int number)
{
    return number++;
}

Look at the generated IL code to see what happens:

IncrementByOne:
    IL_0000:  ldarg.0        // load 'number' onto stack
    IL_0001:  dup            // copy number - this is the reason for the
                             // postfix ++ behavior
    IL_0002:  ldc.i4.1       // load '1' onto stack
    IL_0003:  add            // add the values on top of stack (number+1)
    IL_0004:  starg.s     00 // remove result from stack and put
                             // back into 'number'
    IL_0006:  ret            // return top of stack (which is
                             // original value of `number`)

The reason the postfix ++ operator returns the original (not the incremented) value is because of the dup statement - the value of number is on the stack twice and one of those copies stays on the stack by the ret statment at the end of the function so it gets returned. The result of the increment goes back into number.

xxbbcc
  • 16,930
  • 5
  • 50
  • 83
  • Downvoter, care to explain your vote? If you think something is wrong with this answer, let me know. – xxbbcc Sep 09 '16 at 13:33
  • The code and your description of it are opposites. You say that the value is returned before the increment, but *read the code*. The ret happens *after* the increment! – Eric Lippert Sep 09 '16 at 13:33
  • 1
    A correct statement would be "the value of the operation is the value of the operand before it is incremented". So the sequence goes: evaluation, increment, return. This is all clearly documented in the specification. – Eric Lippert Sep 09 '16 at 13:35
  • @EricLippert Ok, I may have mis-phrased it. I thought I wrote it out correctly. I'll update the answer. – xxbbcc Sep 09 '16 at 13:36
  • Would it had been easier to say that at the point the method returns, the value on the stack is the value of `number` _before_ the increment (and therefore is the return value). The incremented value has been stored in the local but not pushed onto the stack. – Scott Perham Sep 09 '16 at 13:43
  • @ScottPerham Perhaps it would've been easier but I didn't think of that. My original phrasing seemed clear to me - it was supposed to mean what you and Eric Lippert were suggesting but looking back, even I see that it was misleading at the least and perhaps downright wrong. – xxbbcc Sep 09 '16 at 18:02
2

Or, to point out the caveman approach...

public static int IncrementByOne(int number)
{
    number++;
    return number;
}
Zach
  • 640
  • 1
  • 6
  • 16
0

The last function post-increments number; If you want immediate incremented value, you can try return ++number;

rt2800
  • 3,045
  • 2
  • 19
  • 26