9

I have three functions, funt1(), funt2(), and funt3().

int funt1()
{
    cout<<"funt1 called"<<endl;
    return 10;
}

int funt2()
{
    cout<<"funt2 called"<<endl;
    return 20;
}

void funt3(int x=funt1(), int y=funt2())
{
    cout << x << y << endl;
}

My main function:

int main()
{
    funt3();
    return 0;
}

When I am calling funt3() in my main() method, why is funt1() is called first, and then funt2()?

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
teacher
  • 1,005
  • 3
  • 15
  • 27
  • 7
    The language does not define what happens first in this case. You should not write code that depends on either order. – asveikau Aug 30 '11 at 21:41
  • Why the downvotes? The practice may be poor but the question is real and well presented. – Amardeep AC9MF Aug 30 '11 at 21:45
  • what i think is that it is because of `comma operator` between `int x=funt1(), int y=funt2()` as for every comma operator its left side is evaluated first... but there is a problem with this approach is that as this expression is evaluated it will `leave single value that is the value of right most side of comma operator`... – teacher Aug 30 '11 at 21:54
  • 5
    @teacher: The comma used to separate function arguments is not an instance of the comma operator. – Benjamin Lindley Aug 30 '11 at 21:55
  • @Benjamin Lindley what is the difference between comma and it's instance can you give me an example... – teacher Aug 30 '11 at 21:58
  • My dear friend, did you even read the answers? – gwiazdorrr Aug 30 '11 at 22:00
  • @teacher: I don't understand your question to me. When a compiler sees a group of expressions separated by commas inside the parentheses of a function call, the C++ grammar determines that it is interpreted as a series of arguments to that function rather than a series of expressions linked by the comma operator. I'm not an expert on grammar, so I can't tell you exactly how that is done. – Benjamin Lindley Aug 30 '11 at 22:12

8 Answers8

3

It depends on your compiler. Others may call funct2() first. Neither C or C++ guarantee the order of evaluation of function arguments.

See Parameter evaluation order before a function calling in C

Community
  • 1
  • 1
Andrew Stein
  • 12,880
  • 5
  • 35
  • 43
2

C++ standard does not define that, so it's totally compiler-specific. That said, you should never rely on an instance of undefined behaviour.

EDIT: if you really want to keep functions invocations as default parameters to reduce the numbers of parameters you have to pass each time I suggest you do the following:

void funt3(int x, int y)
{
    cout<<x<<y<<endl;
}

void funt3(int x)
{
    funt3(x, funt2());
}

void funt3()
{
    funt3(funt1());
}
gwiazdorrr
  • 6,181
  • 2
  • 27
  • 36
  • 5
    I don't see any undefined behavior in the original post. *Unspecified* behavior, yes -- but that's different from undefined. – Jerry Coffin Aug 31 '11 at 03:56
1

The language does not require any particular order. The order used will be compiler dependent.

Avi Berger
  • 2,068
  • 1
  • 18
  • 13
0

compiler dependent. it maybe funt1 then funt2 then funt3 or any other combination.

teacher
  • 1,005
  • 3
  • 15
  • 27
0

As noted by everybody else, the order of evaluation of function parameters is unspecified by the C++ standard. This allows each compiler to choose an optimal order, whether that order is determined by convenience or efficiency.

You may even find that the order can change based on the optimization flags you give to your compiler.

In order to guarantee the sequence of the function calls you need to introduce a sequence point between the calls. I accomplish this below by creating two different versions of the function, so that only one function call is made as a parameter.

void funt3(int x, int y=funt2())
{
    cout << x << y << endl;
}

void funt3()
{
    int x = funt1();
    funt3(x);
}
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
0

It is because the parameters of funt3(sp?) needs to work out x then y. i.e. funt1() then funt2() before considering the contents of funt3.

roalz
  • 2,699
  • 3
  • 25
  • 42
Ed Heal
  • 59,252
  • 17
  • 87
  • 127
0

Compiler specific and from there it goes down to the CPU. CPU might branch that call into separate branches, it might try to do some predictions, or if the CPU thinks func1 is way faster than func2 it will run func1 and a bunch of other operations before func2 so it optimizes.

Ryan Ternier
  • 8,714
  • 4
  • 46
  • 69
0

As C++ standard doesn't define the order so it's depend on compiler.

You can simply try a few popular c++ compilers: GCC, VS2008/VS2010 etc. Then you will see totally different result.

RoundPi
  • 5,819
  • 7
  • 49
  • 75