By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,903 Members | 1,856 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,903 IT Pros & Developers. It's quick & easy.

order of eveluation of functions

P: n/a
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?

Jun 16 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
In article <11*********************@e9g2000prf.googlegroups.c om>,
Ravi <ra*********@gmail.comwrote:
>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)

--
Dave Vandervies dj******@csclub.uwaterloo.ca
Well, it's evidently clear that the next standard definition of CHAR_BIT
could well lead to important breakthroughs in Chemistry and Particle Physics.
--Peter Nilsson in comp.lang.c
Jun 16 '07 #2

P: n/a
Ravi wrote:
>
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?
Unspecified.

--
pete
Jun 16 '07 #3

P: n/a
On Jun 16, 10:22 am, Ravi <ra.ravi....@gmail.comwrote:
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.

Jun 16 '07 #4

P: n/a
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 16 '07 #5

P: n/a
CryptiqueGuy said:
On Jun 16, 10:22 am, Ravi <ra.ravi....@gmail.comwrote:
>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))
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;

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 16 '07 #6

P: n/a
Ravi wrote:
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).

Jun 16 '07 #7

P: n/a
Ravi wrote:
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.
Jun 16 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.