# call_proc

Definition: | call_proc(integer rid, sequence params)
Description: | Dynamically invoke the user-defined procedure with routine_id rid. rid: a valid result from routine_id(). params: a sequence of argument values of length n, where n is compatible with the number of arguments required by the procedure identified by 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. If you need a routine to be invoked whenever a particular event occurs, the easiest way is to use a routine_id. Historically, a routine_id was the only way to effect a forward call, but that is no longer the case. If the procedure identified by 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: |
    procedure foo(integer fn, string s)
        puts(fn, s)
    end procedure
    integer r_foo = routine_id("foo")

    foo(1,"Hello World\n")                  -- direct/static call
    call_proc(r_foo,{1,"Hello World\n"})    -- indirect/dynamic call (equivalent)
    r_foo(1,"Hello World\n")                --        ""                  ""      on 0.8.1+
Implementation: | via :%opCallProc 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_func, routine_id