EVAL$( code$ )

Description:

Liberty BASIC now has two functions for evaluating BASIC code inside a running program. The eval() function evaluates the code and returns a numeric value, and the eval$() function works the same way but returns a string value. Both will execute the very same code, but the string function converts the result to a string if it isn't already one, and the numeric version of the function converts it to numeric values.

Evaluating to a string

Here we show how to evaluate code to a string, and what happens if you try to evaluate it to be a number.

'Let's evaluate some code that produces a non-numeric result

a$(0) = "zero"

a$(1) = "one"

a$(2) = "two"

code$ = "a$(int("+str$(rnd(1))+"*3))"

print "We will evaluate the code: "; code$

result$ = eval$(code$)

print result$

'Now let's use the eval function, which effectively does a

'val() to the result of the calculation. Converting a non

'numeric string to a numeric value results in zero.

result = eval(code$)

print result

Evaluating to a number

Here's an example of the most common type of code evaluation users will want to do: Numeric computation. Let's just make a short example that asks you to type an expression to evaluate.

'ask for an expression

input "Type a numeric expression>"; code$

answer = eval(code$)

print answer