Solved

Order of function call

Posted on 2005-04-14
214 Views
hi everyone,
Kernighan & Ritchie (in the book the C programming language) says that the order of a function call in an expression cannot be evaluated (section 2.12, third and fourth paragraph)
for eg :
x = f() + g() * h();

the book says any of these functions can be evaluated based on the compiler.

Please explain why is this only for functions,
as everybody knows that when we say

a = 1;
b = 5;
c = 10;
d = a + b * c;

the order of evaluation is b*c (viz = z, say) first and
then a+z (viz = y, say) and then d = y

thus some method of evaluation of expression is used.
such as
1 -> address of a is stored in operand stack (od),
2 -> '+' is pushed into operator stack (otr),
3 -> address of b is pushed into od,
4 -> compares the precedence of the next operator and top
of otr stack, if great pushes '*' into otr stack and
proceeds with next operand
5 -> address of c is pushed into od,
6 -> end of expression, so resolves b and c performes
operation '*' and so on....
thus the order of evaluation is b c and then a.

in the above x = f() + g() * h(),
the function pointers an be stored in the operand stack, thus the order can be dermined.
0
Question by:libin_v

LVL 45

Expert Comment

The comment by K&R is a reference to the processing stack.

a+b*c is a+(b*c)

this can be effected on the stack as

abc*+

or

bc*a+

Replacing a, b, and c with the function calls in your example, you can see that there are several wasy to build the stack, so the functions will likely be called in the order that the compiler intends to place the values on the stack.

Note also that the Associative Laws of albegra also allow more combinations than just these two.

Kent
0

LVL 5

Author Comment

hi kdo,

>Replacing a, b, and c with the function calls in your example, >you can see that there are several wasy to build the stack, so >the functions will likely be called in the order that the compiler >intends to place the values on the stack.

>Note also that the Associative Laws of albegra also allow more >combinations than just these two.

Can u please elaborate on the above said statements. (I didnt understand them).
0

LVL 45

Accepted Solution

abc*+
=
f()g()h()*+

This notation is called "Reverse Polish".  It's how stacks operate and most expression evaluators use a stack.

abc*-

As the expression is being evaluated (left to right) the following occurs:

The first expression is put on the stack.

a

The '-' is held until the evaluator can determine any precedence issues.  The next token is 'b'.  It's put on the stack.

ab

The next operator is '*'.  It has a higher precedence that the '-' being held, so the evaluation continues.  The 'c' processed next and added to the stack.

abc

We've now exhausted the input.  So the items that were "held" by the evaluation's recursive process are handled.  The most recent object is the '*'  It's put on the stack.

abc*

As the recursive process unwinds, the '+' that was held initially is added to the stack.

abc*+

In actuality, the '*' and '+' aren't really added to the stack.  This is done for "demonstration purposes".  When we add operators to the stack for the picture's sake, the system will actually perform the math.  When the '*' is added, the result is this:

becomes

ad   (where d is the product of b and c)

Continuing processing with the '+' results in:

becomes

e   [where e is the sum of a and d]

Shown in traditional fashion, what we have is:

a + b * c
a +    d
e

However, if you replace a, b, and c with function calls, you can wind up with different results because there's more than one way to build the logic tree.  Some parsers will follow the model that I've shown, others will vary.  When the function is called in the evaluation process determines how the tree is built.

Kent
0

LVL 5

Author Comment

fine kdo,
i understood this,

but  Kernighan & Ritchie (in the book the C programming language) says in section 2.12, third -> "C, like most languages, does not specify the order in which the operands of an operator are evaluated. (the exception are &&, ||, ?: and ','.)".

I didnt understand the exceptions, please explain how the exceptions are evaluted.
0

LVL 5

Author Comment

Kdo,
Do not bother to answer the exception part, I understood it.

0

Featured Post

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.