Dynamic Function Calls

Compiler (edit 879) language extension: dynamic function calls. These are function calls that are dynamically bound—i.e. resolved—at runtime rather than at compile time.

To define a function that can be dynamically bound, insert the new keyword "dynamic" in front of the Function keyword in the function definition.

Dynamic Function Fn'Foo(...)

    ...

EndFunction

 

Tagging a function for dynamic binding like this does not interfere with traditional statically bound calls to the function; it simply opens up the additional possibility of dynamic binding.

To call the function with dynamic binding, use one of the new system functions DYNFUNC() or DYNFUNC$() ...

DYNFUNC(funcname,arg1,...,argN)   ! numeric function

DYNFUNC$(funcname,arg1,...,argN)  ! string function

These are analogous to the XFUNC() and XFUNC$() system functions (used to dynamically call external functions implemented as SBX modules), except that here the target function is part of (i.e. compiled within) the current program. As with XFUNC()/XFUNC$(), the first argument (funcname here) is a string expression resolving at runtime to the name of the function to call.

Examples

a$ = DYNFUNC$("Fn'Foo$",arg1,arg2)

 

n = 27

f$ = "Fn'Foo" + str(n) + "$"

rc$ = DYNFUNC$(f$,arg1,arg2,arg3)

? DYNFUNC(Fn'Get'FuncName$(x),arg1,arg2)

 

The first example calls the string function Fn'Foo$(arg1,arg2), setting the return value to the variable rc$.

A new BASIC error code, 72, undefined dynamic function, has been defined and added to the ERRMSG.xxx files. It will be triggered by an attempt to to dynamically call a function which is not declared dynamic within the current program.

Note that in all cases, the choice of DYNFUNC() vs DYNFUNC$() determines whether the function is expected to return a string or numeric value. Since the type of a function is determined by the presences of a "$" suffix, the presence or absence of the suffix on the DYNFUNC keyword should agree at runtime with the name of the target function. In other words, use DYNFUNC() to call numeric functions, and DYNFUNC$() to call string functions. Neither the compiler nor the runtime system will complain if you mix types, but the results are likely to disappoint.

Also note that DYNFUNC() and DYNFUNC$() only work with functions, not procedures, and that as with other system functions—and unlike user-defined functionscannot be invoked with the CALL statement.

Compatibility: RUN / SBX modules with Dynamic functions declared are fully compatible with earlier versions of A-Shell. But an attempt to call them from within an A-Shell session earlier than 6.5.1647.0 will generate an unsupported function error (35).