order of eveluation of functions

R

Ravi

Suppose we are given a statement:
f1(23,1) * f2(3) + f(4)
can you please tell what is the order of evaluation of these functions?
 
D

Dave Vandervies

Suppose we are given a statement:
f1(23,1) * f2(3) + f(4)
can you please tell what is the order of evaluation of these functions?

No.


dave
(that's a serious answer)
 
C

CryptiqueGuy

Suppose we are given a statement:
f1(23,1) * f2(3) + f(4)

Order of evaluation of the operands of an operator is unspecified.
Though your expression will be grouped as something like:
((f1(23,1) * f2(3)) + f(4))

Here, you don't know if f(4) is evaluated first or f1(23,1)*f2(3) is
evaluated first.
Even while evalauating f1(23,1)*f2(3), you don't know if f1(23,1) is
evaluated first or f2(3) is evaluated first.

Moral:
Never write any codes which rely on the order of evlauation of the
operands of the operator, atleast in C.
can you please tell what is the order of evaluation of these functions?

No, I can't. I had already cited the reason.
 
R

Richard Heathfield

Ravi said:
Suppose we are given a statement:
f1(23,1) * f2(3) + f(4)
can you please tell what is the order of evaluation of these
functions?

It's up to the implementation. The order of evaluation is unspecified.
 
R

Richard Heathfield

CryptiqueGuy said:
Order of evaluation of the operands of an operator is unspecified.
Though your expression will be grouped as something like:
((f1(23,1) * f2(3)) + f(4))

Only in precedence terms, not in evaluation order terms.
Here, you don't know if f(4) is evaluated first or f1(23,1)*f2(3) is
evaluated first.
Even while evalauating f1(23,1)*f2(3), you don't know if f1(23,1) is
evaluated first or f2(3) is evaluated first.

There is no particular reason why f1() and f2() must be evaluated
adjacently. Here's one legal order of evaluation:

T1 = f2(3);
T2 = f(4);
T3 = f1(23, 1);
T4 = T3 * T1;
T5 = T4 + T2;
 
J

Johan Bengtsson

Ravi said:
Suppose we are given a statement:
f1(23,1) * f2(3) + f(4)
can you please tell what is the order of evaluation of these functions?
Any of the following:

f1, f2, f
f1, f, f2
f2, f1, f
f2, f, f1
f, f1, f2
f, f2, f1

Some orders might be more probable than others. I would guess at f being
last in more cases than not (because there is less need to store
intermediate values in that case) - but there is no guarantee about the
order of evaluation. The cases where f is in the middle are probably
very unlikely (but is not completely out of chance).
 
M

Martin Ambuhl

Ravi said:
Suppose we are given a statement:
f1(23,1) * f2(3) + f(4)
can you please tell what is the order of evaluation of these functions?

Only your implementation knows. The fact is that all is required is
that each of the functions be evaluated before its value is used.
People are sometimes confused by the precedence rules which they derive
from the grammar and imagine they imply something about the order of
evaluation of those functions. That is an error.

Consider

double foo(void)
{
double tmp1, tmp2, tmp3;
double f1(double,double);
double f2(double);
double f(double);

tmp1 = f1(23,1); /* note order: left to right */
tmp2 = f2(3);
tmp3 = f(4);
return tmp1*tmp2 + tmp3;
}

and
double foo(void)
{
double tmp1, tmp2, tmp3;
double f1(double,double);
double f2(double);
double f(double);

tmp3 = f(4); /* note order: right to left */
tmp2 = f2(3);
tmp1 = f1(23,1);
return tmp1*tmp2 + tmp3;
}

Notice that the result, apart from any unspecified side effects, does
not depend on the order of the function calls.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top