and a further code that intensively uses one of these functions, but the choice which function is to be used depends on the input parameter selected by the program user. Is there any MATHEMATICA language construct, similar to “C” pointers to functions, which allows one to avoid rewriting several versions of the code (for every function separately) or frantically check the user parameter at every call of the function, to decide which function is to be used? Or, is there any other recommended way to handle the above situation? Lesław
In Mathematica, everything is an expression. So, the expression fun1 is effectively the “function pointer” that you want. It’s an expression with down values that specify how to evaluate when applied to arguments. Let’s make this concrete:
and in the further code I just call fun[arg]. Is this an efficient solution? I wonder when exactly the above substitutions in Which[] are executed (before calling any fun[] or during such calls)?, and are the functions fun1[], fun2[] or fun3[] called directly, or indirectly (by indirectly I mean that first fun[] is called and then it calls any of fun1[], fun2[] or fun3[])?
Lesław
You would do something more appropriate than just Null in the fall-through case. If, Switch, and Which are special forms that hold their arguments unevaluated, so you don’t need to worry about unexpected evaluation as a side-effect.
But, another way to view this is as a parameterization to your function, like Hans suggested. An alternate way to do that would be: