HP Forums
question : a trick to call function from a variable - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: question : a trick to call function from a variable (/thread-7002.html)



question : a trick to call function from a variable - primer - 10-08-2016 10:12 PM

hello,
I have a function name in a variable, and I would like to call it, some one know how to do ?
example :
Code:

EXPORT myfct(x)
BEGIN
  PRINT(x);
END;

EXPORT test(fct)
BEGIN
  local call:=fct+"(123)";
  EVAL(call);
END;
Now, If I call test("myfct"), Iwant the function myfct to be called (with parameter 123)
But it does not works as I wanted :
it returns "myfct(123)" but does not execute it...
if someone can help me..
Thank you.


RE: question : a trick to call function from a variable - StephenG1CMZ - 10-08-2016 11:09 PM

I think
EVAL(EXPR(call))
Or perhaps just
EXPR(call)
Will do what you want.


RE: question : a trick to call function from a variable - primer - 10-09-2016 11:14 AM

Hi StephenG1CMZ,
Thank you very much, it's OK.

By the way, a note for thoses who may want to use such callback trick : if you want to use string as function parameters, you have to protect them with backslashs (\").

For example par2 accept string abcd

Code:
EXPORT myfct(par1, par2)
BEGIN
  PRINT(par1+" and "+par2);
END;

EXPORT test(fct)
BEGIN
  local call:=fct+"(123,\"abcd\")";
  EVAL(call);
END;