Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.


10.7.1 Evaluation

When you've quoted an expression you have the address of the code which calculates the value of the expression. To evaluate the expression you pass this address to the Eval function. So now we have a round-about way of calculating the value of an expression. (If you have a GB keyboard you can get the ` character by holding down the ALT key and pressing the ' key, which is in the corner just below the ESC key. On a US and most European keyboards it's on the same key but you don't have to press the ALT key at the same time.)

PROC main()
  DEF adr, x, y
  x:=0; y:=0
  adr:=`1+(fred(x,1)*8)-y
  x:=2; y:=7
  WriteF('The value is \d\n', Eval(adr))
  x:=1; y:=100
  WriteF('The value is now \d\n', Eval(adr))
ENDPROC

PROC fred(a,b) RETURN (a+b)*a+20

This is the output that should be generated:

The value is 202
The value is now 77

This example shows a quite complicated expression being quoted. The address of the expression is stored in the variable adr, and the expression is evaluated using Eval in the calls to WriteF. The values of the variables x and y when the expression is quoted are irrelevant--only their values each time Eval is used are significant. Therefore, when Eval is used in the second call to WriteF the values of x and y have changed so the resulting value is different.

Repeatedly evaluating the same expression is the most obvious use of quoted expressions. Another common use is when you want to do the same thing for a variety of different expressions. For example, if you wanted to discover the amount of time it takes to calculate the results of various expressions it would be best to use quoted expressions, something like this:

DEF x,y

PROC main()
  x:=999; y:=173
  time(`x+y,     'Addition')
  time(`x*y,     'Multiplication')
  time(`fred(x), 'Procedure call')
ENDPROC

PROC time(exp, message)
  WriteF(message)
  /* Find current time */
  Eval(exp)
  /* Find new time and calculate difference, t */
  WriteF(': time taken \d\n', t)
ENDPROC

This is just the outline of a program--it's not complete so don't bother running it. The complete version is given as a worked example later (see 20 Timing Expressions).


Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.