ameen
ameen

Reputation: 851

Order of function call

for the expression

(func1() * func2()) + func3()

will func1() * func2() be evaluated first as it has brackets or can the functions be called in any order like

first func3() and then (func1() * func2())

Upvotes: 8

Views: 5477

Answers (6)

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84159

These calls can be made in any order. You want to learn about C++ sequence points C++ sequence points.

Upvotes: 4

Prasoon Saurav
Prasoon Saurav

Reputation: 92864

Precedence of operators has got nothing to do anything with the order of evaluation of operands.

The C or C++ Standard doesn't determine the order in which the functions would be called. .

The order of evaluation of subexpressions, including

  • the arguments of a function call and
  • operands of operators (e.g., +, -, =, * , /), with the exception of:
    • the binary logical operators (&& and ||),
    • the ternary conditional operator (?:), and
    • the comma operator (,)

is Unspecified

For example

  int Hello()
  {
       return printf("Hello"); /* printf() returns the number of 
                                  characters successfully printed by it
                               */
  }

  int World()
  {
       return printf("World !");
  }

  int main()
  {

      int a = Hello() + World(); //might print Hello World! or World! Hello
      /**             ^
                      | 
                Functions can be called in either order
      **/
      return 0;
  } 

Upvotes: 10

John Dibling
John Dibling

Reputation: 101456

It's natural to think that A+B is evaluated before C in this psudocode:

(A+b)*C

But in fact this is not so. The Standard says that the order of evaluation for all expressions is "Unspecified", unless otherwise specified by the Standard:

5/4 [expr]:

Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified

The Standard then goes on to identify a parenthesized expression as a "Primary expression" but does not specify the order of evaluation for Primary expressions. (5.1/5).

In Standardese, "Unspecified" does not mean "Undefined." Rather it means "Implementation Defined, but no documentation is required." So you might not even be able to say what the order of evaluation is for a specific compiler.

Here is a simple program illustrating the behavior:

#include <iostream>
#include <string>
using namespace std;

class Foo
{
public:
    Foo(const string& name) : name_(name) {++i_; cout << "'" << name << "'(" << i_ << ")\n"; };
    operator unsigned() const { return i_; }
    Foo operator+(const Foo& rhs) const { string new_name = name_; new_name += "+"; new_name += rhs.name_; return Foo(new_name); }
private:
    string name_;
    static unsigned i_;
};

unsigned Foo::i_ = 0;

int main()
{
    (Foo("A") + Foo("B")) + Foo("C");
}

On my MSVC10 running in Debug/x64 on Win7, the output happened to be:

'C'(1)
'B'(2)
'A'(3)
'A+B'(4)
'A+B+C'(5)

Upvotes: 2

Paul R
Paul R

Reputation: 212959

You can't make any assumptions about the order in which these functions will be called. It's perfectly valid for the compiler to call these functions in any order, assign the results to temporaries, and then use these temporary values to calculate the result of the expression.

Upvotes: 6

James
James

Reputation: 5425

Parenthesis in C/C++ force order of operations. func1() * func2() will be added to func3(), but the compiler can choose to call the functions in whatever order it wishes before passing in the results to the multiplication / addition operation.

Upvotes: 2

Benjamin Lindley
Benjamin Lindley

Reputation: 103693

The functions can be called in any order.

Upvotes: 16

Related Questions