Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the behavior of return x++; defined?

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?

like image 930
patros Avatar asked Mar 04 '10 16:03

patros


People also ask

What does return do in a function?

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.

What is returned by functions that don't have a return statement?

If a function doesn't specify a return value, it returns None .

What is the difference between ++ i and ++ i?

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.

What is pre and post increment?

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++;


1 Answers

Yes, it's equivalent to:

int bar() {   int temp = x;   ++x;   return temp; } 
like image 133
Peter Alexander Avatar answered Sep 21 '22 01:09

Peter Alexander