Try OpenEdge Now
skip to main content
ABL Essentials
Defining Functions : Defining a function : Making run-time references with DYNAMIC-FUNCTION
 

Making run-time references with DYNAMIC-FUNCTION

ABL lets you construct a reference to a function at run time, using a built-in function called DYNAMIC-FUNCTION. This is the syntax:

Syntax

DYNAMIC-FUNCTION ( function [ IN handle][ , parameters] )
Like a function reference itself, the DYNAMIC-FUNCTION function can appear anywhere in your procedure code where an expression of that data type could appear.
The first parameter to DYNAMIC-FUNCTION is the name of the function to invoke. This procedure can be a quoted literal or an expression, such as a variable that evaluates to a valid function name.
Following this, you can optionally include an IN handle phrase to direct the AVM to execute the function in a persistent procedure handle. In this case, the handle must be an actual field or variable name of HANDLE data type, not an expression.
If the function itself takes any parameters, you pass those as additional parameters to DYNAMIC-FUNCTION, in the same form that you would pass them to the function itself.
DYNAMIC-FUNCTION gives you the flexibility to have code that can execute different function names depending on the situation, since the function parameter can be a variable. You might have, for example, several different functions that do parallel work for different categories of data in a loop you're iterating through. However, that is of perhaps limited value because all the functions you invoke must have the same signature.
The most common use of DYNAMIC-FUNCTION is for one of two other reasons:
*If you want to reference a function without going to the trouble of defining a prototype for it, you can do this with DYNAMIC-FUNCTION. Because the AVM has no way of knowing what the name of the function is or its return type, it cannot look for a prototype for it and therefore does not give you an error as it would if you had a static reference to a function with no prior declaration. By the same token, it cannot provide you with any helpful warnings if your reference to the function is not valid.
*If you want to invoke a function in a number of different persistent procedures, you can easily do this with DYNAMIC-FUNCTION since the procedure handle to run it is part of the function reference, and not defined in the prototype. In this way, you can run a function in different persistent procedure instances depending on the circumstances. If each of those procedures represents an application object (such as a window, frame, browse, or query), then it can be very powerful to invoke the same function in different procedure handles representing those objects.