## Evaluating a user-defined function with EVAL

Expand Messages
• I don t seem to be able to evaluate a user-defined function with EVAL. The program below prints out 4 rather than the expected 5.41421356 (obviously the
Message 1 of 7 , Jul 2, 2007
• 0 Attachment
I don't seem to be able to evaluate a user-defined function with
EVAL. The program below prints out 4 rather than the expected
5.41421356 (obviously the program is just an example; in the real
application 'myfun' would be more complicated than a simple sqr).

Is this a fundamental limitation, or am I doing something wrong?

Richard.

a\$ = "1 + myfun(2) + 3"
print eval(a\$)
end

function myfun(myval)
myfun = sqr(myval)
end function
• It s seeing myfun(2) as an array and not a function. Try myfun(25) and you ll get a subscript out of range error. Place the function value in a numerical
Message 2 of 7 , Jul 2, 2007
• 0 Attachment
It's seeing myfun(2) as an array and not a function. Try myfun(25)
and you'll get a subscript out of range error. Place the function
value in a numerical variable and then just use that variable in the
function.

' Code Begins
a = myfun(2)
a\$ = "1 + a + 3"
print eval(a\$)
end

function myfun(myval)
myfun = sqr(myval)
end function
' Code Ends

Janet

--- In libertybasic@yahoogroups.com, "Richard Russell" <yahoo@...>
wrote:
>
> I don't seem to be able to evaluate a user-defined function with
> EVAL. The program below prints out 4 rather than the expected
> 5.41421356 (obviously the program is just an example; in the real
> application 'myfun' would be more complicated than a simple sqr).
>
> Is this a fundamental limitation, or am I doing something wrong?
>
> Richard.
>
> a\$ = "1 + myfun(2) + 3"
> print eval(a\$)
> end
>
> function myfun(myval)
> myfun = sqr(myval)
> end function
>
• ... Thanks for the suggestion, but it doesn t really help in my situation because the function name itself will be entered by the user (or read from a file)
Message 3 of 7 , Jul 2, 2007
• 0 Attachment
--- In libertybasic@yahoogroups.com, "JanetTerra" wrote:
> Place the function value in a numerical variable and then
> just use that variable in the function.

Thanks for the suggestion, but it doesn't really help in my
situation because the function name itself will be entered by the
user (or read from a file) and isn't known 'ahead of time'. The
idea is to plot an arbitrary equation which may include numeric
functions that aren't built intrinsically into LB (e.g. hyperbolic
trig functions, factorial etc.). Writing user-defined functions for
each of these is the obvious solution, but EVAL needs to be able to
evaluate them just like the intrinsic functions.

The only workaround I can see is to scan (using instr) the
expression string for every possible function name, but it would
require so much parsing of the expression that the benefit of using
EVAL is largely wasted.

This may be a question for Carl, but since EVAL is supposed to
evaluate a BASIC expression isn't its failure to recognise a user-
defined function a bug?

Richard.
• Well, now I m very curious. If the function isn t written into the code, how does the code use the function? Can you give a small, non- gui example? Janet
Message 4 of 7 , Jul 2, 2007
• 0 Attachment
Well, now I'm very curious. If the function isn't written into the
code, how does the code use the function? Can you give a small, non-
gui example?

Janet

--- In libertybasic@yahoogroups.com, "Richard Russell" <yahoo@...>
wrote:
>
> --- In libertybasic@yahoogroups.com, "JanetTerra" wrote:
> > Place the function value in a numerical variable and then
> > just use that variable in the function.
>
> Thanks for the suggestion, but it doesn't really help in my
> situation because the function name itself will be entered by the
> user (or read from a file) and isn't known 'ahead of time'. The
> idea is to plot an arbitrary equation which may include numeric
> functions that aren't built intrinsically into LB (e.g. hyperbolic
> trig functions, factorial etc.). Writing user-defined functions
for
> each of these is the obvious solution, but EVAL needs to be able to
> evaluate them just like the intrinsic functions.
>
> The only workaround I can see is to scan (using instr) the
> expression string for every possible function name, but it would
> require so much parsing of the expression that the benefit of using
> EVAL is largely wasted.
>
> This may be a question for Carl, but since EVAL is supposed to
> evaluate a BASIC expression isn't its failure to recognise a user-
> defined function a bug?
>
> Richard.
>
• ... Here is an example of a program that I would *like* to work! The DATA statement is used, for simplicity, in lieu of requesting the expression string from
Message 5 of 7 , Jul 2, 2007
• 0 Attachment
--- In libertybasic@yahoogroups.com, "JanetTerra" wrote:
> Well, now I'm very curious. If the function isn't written into
> the code, how does the code use the function? Can you give a
> small, non-gui example?

Here is an example of a program that I would *like* to work! The
DATA statement is used, for simplicity, in lieu of requesting the
expression string from the user or reading it from a file. What the
program attempts to do is tabulate the values of the expression for
values of x from 0.0 to 1.0.

Imagine the expression being a lot more complicated, and entered by
the user or read from a file at run time. Also assume that many
more user-defined functions are available for use in the expression.

Have I missed something obvious?

Richard.

data "sinh(x) + cosh(x) - 1.0"

for x = 0.0 to 1.0 step 0.01
print x, eval(expression\$)
next x
end

function sinh(x)
sinh = (exp(x)-exp(0-x))/2
end function

function cosh(x)
cosh = (exp(x)+exp(0-x))/2
end function
• ... I d say no, except maybe as a documentation bug. Because of limitations in the underlying architecture it isn t really practical to implement the
Message 6 of 7 , Jul 2, 2007
• 0 Attachment
--- In libertybasic@yahoogroups.com, "Richard Russell" <yahoo@...>
wrote:
> This may be a question for Carl, but since EVAL is supposed to
> evaluate a BASIC expression isn't its failure to recognise a user-
> defined function a bug?

I'd say no, except maybe as a documentation bug. Because of
limitations in the underlying architecture it isn't really practical
to implement the functionality you are asking for. However this
should pose no real problem with Run BASIC and LB5.

-Carl
• Stefan A couple of years ago I made a function plot program. The program has one textbox and the content of it is evaluated with .The program also has a
Message 7 of 7 , Jul 2, 2007
• 0 Attachment
Stefan
A couple of years ago I made a function plot program. The program has one textbox and the content of it is evaluated with <eval>.The program also has a random access file containing all my functions sin, cosh, tanh or anything you can think of. By choosing a number in the raf-file the function is copied to the textbox and evaluated. I also have a text window showing all the functions stored in the raf-file. The whole thing is very simple and has been good enough for me.
I have used eval a lot and it is a very nice feature in LB4.03. I have learned through the years that if the program does not perform exactly as you had hoped you just change your plans a little bit and you can get pretty close to what you initially had thought.
regards
Ingemar

----- Original Message -----
From: Stefan Pendl
To: libertybasic@yahoogroups.com
Sent: Monday, July 02, 2007 7:37 PM
Subject: AW: [libertybasic] Re: Evaluating a user-defined function with EVAL

>>Imagine the expression being a lot more complicated, and entered by
>the user or read from a file at run time. Also assume that many
>more user-defined functions are available for use in the expression.
>
>Have I missed something obvious?
>

Sorry, there is nothing to miss :-(

Lb just does not support custom functions with EVAL() or EVAL\$().
This feature is on the wish-list since Carl included EVAL(), we will see what we get with LB5 ;-)

The only way currently is to build a custom preprocessor, which will replace the custom functions with their results.

---
Stefan Pendl

__________________________________ Die besten Tipps und Tricks fürs Grillen. BE A BETTER GRILLMEISTER! www.yahoo.de/clever

[Non-text portions of this message have been removed]
Your message has been successfully submitted and would be delivered to recipients shortly.