Skip to Content

EVAL (EV.)

A function which applies the interpreter’s expression evaluation program to the characters held in the argument string.

X=EVAL("X^Q+Y^P") X=EVAL"A$+B$" X$=EVAL(A$)

In effect, you pass the string to BBC BASIC(Z80)‘s evaluation program and say ‘work this out’.

You can use this function to accept and evaluate an expression, such as a mathematical equation, whilst the program is running. You could, for instance, use it in a ‘calculator’ program to accept and evaluate the calculation you wished to perform. Another use would be in a graph plotting program to accept the mathematical equation you wished to plot.

The example below is a ‘bare bones’ calculator program which evaluates the expression typed in by the user.

10 PRINT "This program evaluates the expression" 20 PRINT "you type in and prints the answer" 30 REPEAT 40 INPUT "Enter an expression" exp$ 50 IF exp$<>"END" PRINT EVAL exp$ 60 UNTIL exp$="END" 70 END

You can only use EVAL to work out functions (like SIN, COS, etc). It won’t execute statements like MODE 0, PRINT, etc.

In the following example, EVAL would print ‘Hello world!’ if you entered ‘FN_FRED’ in response to the prompt. The program will continue until an error occurs (pressing ESC for example).

10 REPEAT 20 INPUT “Enter an expression: “,a$ 30 PRINT EVAL a$ 40 UNTIL FALSE 50: 60 DEF FN _FRED 70 PRINT “Hello World!” 80 =-1

Example run (user entry in bold):

>RUNENTER
Enter an expression: 2*5ENTER
10
Enter an expression: FN_FREDENTER
Hello World!
Enter an expression: JIMENTER
No such variable at line 20
>

Syntax

<n-var>=EVAL(<str>) <s-var>=EVAL(<str>)

Associated Keywords

STR$, VAL

Last updated on