If I have for example a class with instance method and variables
class Foo { ... int x; int bar() { return x++; } };
Is the behavior of returning a post-incremented variable defined?
A return statement ends the execution of a function, and returns control to the calling function. Execution resumes in the calling function at the point immediately following the call. A return statement can return a value to the calling function.
If a function doesn't specify a return value, it returns None .
The only difference is the order of operations between the increment of the variable and the value the operator returns. So basically ++i returns the value after it is incremented, while i++ return the value before it is incremented. At the end, in both cases the i will have its value incremented.
Pre-incrementation means the variable is incremented before the expression is set or evaluated. Post-incrementation means the expression is set or evaluated, and then the variable is altered. It's easy to think of it as a two step process. b = x++;
Yes, it's equivalent to:
int bar() { int temp = x; ++x; return temp; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With