Can't believe that this works!

Clarity is Worth fighting forClarity is Worth fighting for 2
I put GetGuess() on the cout line and I can’t believe that it actually works. I thought the program would have a bug where "Your guess was: " would be printed first before it asks you for your guess. I’d be interested to know how the compiler steps through the program.

Hi Chris
I wrote the same code as you did. I was sure it will print "Your guess was: " before executing GetGuess. But it works right. I really would like to know why it works correctly.

1 Like

Here are some examples of using the same coding style.
GetGuess and GetCurrentTry
How I removed the side-effects

1 Like

Thanks, I’ll look at them when I get the chance

The rules about the evaluation order of operators and their operands are complicated, and in many cases the order is actually unspecified (meaning that you should never rely on a particular order, even when always using the same compiler):

http://en.cppreference.com/w/cpp/language/eval_order

Because of that, it is generally advised to avoid code like that, and instead make things clearer by first calling the function(s), then operating on the results. This helps avoid mistakes and makes life easier for other developers and yourself when reading your code at a later date.

1 Like

Privacy & Terms