0

Possible Duplicates:
What belongs in an educational tool to demonstrate the unwarranted assumptions people make in C/C++?
In C99, is f()+g() undefined or merely unspecified?

In the statement, "function1() + function2(); "which function will be called first?

Community
  • 1
  • 1
mohit
  • 11
  • 1
  • 3
    Evaluation order is undefined. Maaany duplicates exist in SO. –  Apr 09 '11 at 16:30
  • what it mean, i could not get – mohit Apr 09 '11 at 16:31
  • Anyways, that is a bad coding style – Lukasz Madon Apr 09 '11 at 16:33
  • 1
    @Muggen No, we agreed in http://stackoverflow.com/questions/3951017/in-c99-is-fg-undefined-or-merely-unspecified that it was unspecified. – Pascal Cuoq Apr 09 '11 at 16:37
  • 1
    @lukas: Writing functions where this matters is worse and the only thing that makes this bad. –  Apr 09 '11 at 16:37
  • mohit, it means 1)It depends on the compiler 2)It is never really an issue if you do everything right. – Cray Apr 09 '11 at 16:56
  • @delnan there is no simple answer. IMO good rule of thumb is to place each call in separate statment. Firsty, readability - veryLongNameOfFucntion() + veryLongNameOfFucntion() + veryLongNameOfFucntion() + ...Secondly, at some point in the future other develepor would change fun1 to depend on fun2 and open a can of bugs. – Lukasz Madon Apr 09 '11 at 17:00
  • @lukas: Well, perfectly pure functions stay perfectly pure functions unless you vastly change their purpose (a bad idea anyway, and forces all users to check their use of it anyway). There's nothing wrong with `x = sin(x) + foo(x) * acceleration(someStruct)`. –  Apr 09 '11 at 17:07
  • @lukas It is perfectly safe to call several functions in the same statement if their side-effects do not interfere. As delnan says, widespread communication through side-effects is bad, not writing `l = cos(a1)*l1 + cos(a2)*l2;`. Backus said that the superiority of Fortan over assembler is that it does not force you to name intermediate quantities (quoting from memory, reference welcome). – Pascal Cuoq Apr 09 '11 at 17:11
  • @delnan Of course, but if this is not the code from 3d engine where performance is critical, I'd put it in separate lines ;). PS "The moral is that writing code that depends on order of evaluations is a bad programming practice in any language." K&R – Lukasz Madon Apr 09 '11 at 17:18
  • @lukas: I never thought a second about performance. Propably because I know enough about assembly language and compiler optimizations to know that there will be no difference. And K&R is completely right. –  Apr 09 '11 at 17:24

1 Answers1

5

The order of evaluation is unspecified.

Read this answer.


Community
  • 1
  • 1
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345