functionti-basic

Ti-basic passing function as an argument to another function


In Matlab you can declare an anonymous function and pass it to another function.

[y] = someFunction(@(x) x.^2 , [a bunch of numbers]);

I'd like to do something similar on my TI-89 calculator. I have a function that takes a "math-function" as one of its arguments and I'm trying to do it like this:

myfunction(3/x,1,2)

and my function looks something like this:

myfunction(f,xl,xu)
Func
local a,b
f(xl)→a
f(xu)→b
Return [a,b]
EndFunc

I know I can input my functions in the "y=" editor and then access them inside the function but I would really like to be able to input the math-function directly as an argument. How can I do this?


Solution

  • The builtin expr function in TI-BASIC can be used to turn a string into an expression. Here's how to implement your function this way:

    myfunction(f,xl,xu)
    Func
    Local a,b,x
    
    xl→x
    expr(f)→a
    
    xu→x
    expr(f)→b
    
    Return [a,b]
    EndFunc
    

    The call to your function will be myfunction("3/x",1,2). Be sure to enclose the definition of f in double quotes so it is treated as a string.