Expand/Shrink

call_func

Definition: object x = call_func(integer rid, sequence params)
Description: Dynamically invoke the user-defined function with routine_id rid.

rid must be a valid result from routine_id(), and params must be a sequence of argument values of length n, where n is compatible with the number of arguments required by the function rid.
x will be the result returned by the function rid.
pwa/p2js: Supported.
Comments: Calling a routine directly is known as a static call: it is determined at compile-time and cannot be changed at run-time. Conversely, invoking a routine via a routine_id is known as a dynamic call: the value of the routine_id can be altered at run-time and hence the routine being called can be altered at run-time. While the trivial example below shows no benefits, the ability to store routine_ids in tables (etc) is much more flexible, and often quite necessary for gui programming. Historically, a routine_id was the only way to effect a forward call, but that is no longer the case.

If function rid does not take any arguments then params should be {}.

As of 0.8.1+, routine_ids are first class, ie callable directly as well as via call_func/proc, see example below.
Example:
function sum2(integer a, inteber b)
    return a+b
end function
integer r_sum2 = routine_id("sum2")

?sum2(1,2)                  -- direct/static call
?call_func(r_sum2,{1,2})    -- indirect/dynamic call (equivalent)
?r_sum2(1,2)                --        ""                 ""      on 0.8.1+
Implementation: via :%opCallFunc in builtins\VM\pcallfunc.e (an autoinclude) - be warned however it is low-level complicated stuff that you do not need to know.
While it works fine, it is rather far from elegant and efficient, and any sensible suggestions for improvement would be most welcome.
Some additional notes on first class routine_ids can be found in the technicalia drop-down of routine_id.
In a way, p2js.js cheats, since JavaScript can pass actual functions around and invoke them just as easily as Phix can do with these integers.
See Also: call_proc, routine_id