CSADPRG_mod4_operand_evaluation_order
this is unedited
process
- variables are evaluated by fetching values from memory
- constants sometimes fetch from memory, other times they're part of the machine language instruction
- if an operand is a parenthesized expression: evaluate all operands and operators first
side effects
- functional side effects: when a function changes either one of its parameters or a global variable
int a = 5;
int fun1() {
a = 17;
return 3;
}
void main() {
a = a + fun1();
}
how to solve?
- disallow functional side effects
- no two-way parameters in functions
- no nonlocal references in functions
- DISADVANTAGES:
- in C all subprograms can only return one value., To return more than one value you would need to place the values in a struct and return the struct.
- Global variables are necessary when efficiency is important and there is a need to avoid parameter passing. An example would be the symbol table used by compilers.
- DISADVANTAGES:
- Write the language definition to state the order of evaluating operands and demand that implementors guarantee that order
- DISADVANTAGES: Limits some compiler optimizations that involve reordering operand evaluations.
- Java's language definition guarantees that operands are evaluated left to right.