Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

Order of function call

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
libin_v
Asked:
libin_v
  • 3
  • 2
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:

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
 
libin_vAuthor Commented:
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
 
Kent OlsenData Warehouse Architect / DBACommented:

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:

abc   [add * operator]

becomes

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

Continuing processing with the '+' results in:

ad    [add + operator]

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
 
libin_vAuthor Commented:
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
 
libin_vAuthor Commented:
Kdo,
     Do not bother to answer the exception part, I understood it.

Thank U for answering.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now