473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

order of eveluation of functions

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
7 1321
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Bhushit Joshipura | last post by:
This post contains one question and one proposal. A. May I know why order of evaluation of arguments is not specified in C/C++? I asked a question in comp.lang.c++ for the following...
14
by: Joerg Schuster | last post by:
Hello, according to http://mail.python.org/pipermail/tutor/2001-July/007246.html the order of function definitions does matter in python. Does anyone know a trick to avoid this? Is there a...
6
by: Ian Sparks | last post by:
I have a python file with a number of functions named with the form doX so : doTask1 doThing doOther The order these are executed in is important and I want them to be executed top-down. They...
12
by: Matias Silva | last post by:
I have this list that i'm ordering by units_unit_id and is there way I can get a natural order so that D4 and D5 comes before D12 Thanks, Matt +---------------+ | units_unit_id |...
2
by: Jesse Engle | last post by:
i'm learning how to do some basic low-level network programming. the site i'm reading talks about "network byte order" and "host byte order". the thing is, it doesn't give an explanation as to what...
25
by: tienlx | last post by:
Hi all, i'm sorry about my poor English ( it isn't my native language). I confused the order which the function in C be evaluted in for() function ? Ex: i have 3 functions : a() b() and c() and...
7
by: pauld | last post by:
Ive got a series of 2 dimensional associative arrays while(list($key, $val) = each ($results)) { {foreach($val as $i=>$v) print "$i = $v"; } } but i want to define the order that the...
16
by: mdh | last post by:
May I ask the group the following: (Again, alas , from K&R) This is part of a function: while ( ( array1 = array2 ) != '\0' ); /* etc etc */ Is this the order that this is evaluated? ...
13
by: Thomas Mlynarczyk | last post by:
Hi, I have this code: class Test { public $status = 'dead'; function __construct() { $this->status = 'alive'; } function __destruct() { echo '<br>__destruct()'; } }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.