3

Possible Duplicate:
function parameter evaluation order

Assuming that I have a function with 4 arguments. Which parameter is considered first for execution and why.

I was trying to understand the , operator's significance used for the function prototype. As is the rule is it the last variable considered first?

Community
  • 1
  • 1
Shash
  • 4,160
  • 8
  • 43
  • 67
  • What do you mean by `'executed'`? – Hunter McMillen May 21 '12 at 17:47
  • Parameter's of a function aren't "executed". – Alex Lockwood May 21 '12 at 17:47
  • 4
    The `,` operator has no significance at all, because the commas in between function argument expressions *are not comma operators*. They're just commas. Other than that, I'm pretty certain this question is a dupe. – Steve Jessop May 21 '12 at 17:48
  • None of the prameters is considered "first". Parameters are just data that you send to the function, it's completely up to you how you use them inside. – cen May 21 '12 at 17:48
  • @SteveJessop I had a question which was quite related to the comma operator. I was confused whether the same operator has any significance even here. – Shash May 21 '12 at 17:52

2 Answers2

5

If we have a function with the following prototype:

int function(int x, int y, int z);

And we call it like so:

function( something_a(), something_b(), something_c() );

We have no ability to presume the order of execution of something_a, something_b and something_c.

On the other hand, we can use the comma operator as follows:

int main() {
    int x;
    something_a(), something_b();
    something_c();
}

In this case, we know that something_a will be called, then something_b, and finally something_c.

In summary, the comma found in a function call, is not the comma operator.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
1
  1. The commas are used as a means of separating the arguments. Commas are not operators.

  2. The ordering of function arguments is dependent on the compiler and how the runtime stack operates. The standard leaves it up to the compiler to determine the order in which arguments are evaluated, so you should not rely on a specific order being kept.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250