Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Programming Question?

int sample()

{

int x = 0;

return x++;

}

what would be the return value of sample(). Pls explain your answer

Update:

Yah...I have tried running the code, but why does sample() return 0, not 1.

4 Answers

Relevance
  • BigRez
    Lv 6
    1 decade ago
    Favorite Answer

    x++ is the post increment operation

    ++x is the preincrement operation.

    So, the value of x is returned prior to the increment. If you can actual examine the code on some systems, there never is an increment operation because the optimizer will notice the post operation and not include it in the executable code.

  • ?
    Lv 6
    1 decade ago

    int sample()

    //tells me the return type is int and the method name is sample, with empty parenthesis tells me nothing is being passed into method.

    int x=0;

    tells me you declared a type int, assigned a variable called x to it, and gave it a value of 0

    return x++

    will return the value of x and then increment it.

    Unless you do ++x

  • 1 decade ago

    0 is the value returned.

  • csanon
    Lv 6
    1 decade ago

    Why don't you try running the code above, and seeing for yourself?

Still have questions? Get your answers by asking now.