#### Archived

This topic is now archived and is closed to further replies.

# surprising order!

This topic is 5884 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

float getNextFloat();

// This code caused me trouble:
vec3 getNextVec3() {
return vec3(getNextFloat(),getNextFloat(),getNextFloat());
}

// I thought that it worked similar to this:
vec3 getNextVec3() {
vec3 r;
r.x = getNextFloat();
r.y = getNextFloat();
r.z = getNextFloat();
return r;
}

I thought that the two methods would give the same result. But no. Why are x, y and z values opposite order in the first getNextVec3() method? ----------- RacingTreme - for fun multiplayer racing: http://users.utu.fi/stkibr Coming soon... [edited by - stefu on September 9, 2002 12:52:26 PM]

##### Share on other sites
The order of evaluation for function (or method) parameters is undefined. If you say "f(x,y,z)" then y could be evaluated first, or it could be z, or it could be x. It depends on the compiler, and can even depend on the compiler''s optimization settings.

##### Share on other sites
Because arguments are pushed on to the stack in right-to-left order, meaning that if you use a return value from a function, that function gets called.

vec3::vec3(float x, float y, float z){}

you should do this if you want the above code to work:
vec3::vec3(float z, float y, float x){}

##### Share on other sites
Ok, thanks.

-----------
RacingTreme - for fun multiplayer racing:
http://users.utu.fi/stkibr
Coming soon...

##### Share on other sites
quote:
Original post by daerid
Because arguments are pushed on to the stack in right-to-left order, meaning that if you use a return value from a function, that function gets called.

I''m not convinced that this behaviour is guaranteed across all compilers (ie. in the ISO/ANSI C++ standard). I thought it was undefined. Do you have evidence of this?

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

1. 1
Rutin
28
2. 2
3. 3
4. 4
5. 5

• 13
• 11
• 10
• 13
• 20
• ### Forum Statistics

• Total Topics
632952
• Total Posts
3009430
• ### Who's Online (See full list)

There are no registered users currently online

×