Try OpenEdge Now
skip to main content
ABL Essentials
Defining Functions : Defining a function : Making a forward declaration for a function : Making a declaration of a function in another procedure
 
Making a declaration of a function in another procedure
Because functions are so generally useful, you might want to execute a function from many procedures when it is in fact implemented in a single procedure file that your application runs persistent. In this case, you can provide a prototype that specifies the handle variable where the procedure handle of the other procedure is to be found at run time. This is the second option in the prototype syntax:

Syntax

[ MAP [ TO ]actual-name] IN proc-handle
The proc-handle is the name of the handle variable that holds the procedure handle where the function is actually implemented. If the function has a different name in that procedure than in the local procedure, you can provide the MAP TOactual-name phrase to describe this. In that case, actual-name is the function name in the procedure whose handle is proc-handle.
To see an example of the CtoF function separated out in this way:
1. Create a procedure with just the function definition in it:
/* h-FuncProc.p -- contains CtoF and possible other useful functions. */
FUNCTION CtoF RETURNS DECIMAL (INPUT dCelsius AS DECIMAL):
  RETURN (dCelsius * 1.8) + 32.
END FUNCTION.
It could also have other internal entries to be used by others that have its procedure handle at run time.
2. Change the procedure that uses the function to declare it IN hFuncProc, its procedure handle. The code has to run h-FuncProc.p persistent or else access its handle if it's already running. In this case, it also deletes it when it's done:
/* h-ConvTemp2.p -- convert temperatures and demonstrate functions. */
DEFINE VARIABLE dTemp     AS DECIMAL    NO-UNDO.
DEFINE VARIABLE hFuncProc AS HANDLE     NO-UNDO.

FUNCTION CtoF RETURNS DECIMAL (INPUT dCelsius AS DECIMAL) IN hFuncProc.

RUN h-FuncProc.p PERSISTENT SET hFuncProc.

REPEAT dTemp = 0 TO 100:
  DISPLAY dTemp LABEL "Celsius"
    CtoF(dTemp) LABEL "Fahrenheit"
    WITH FRAME f 10 DOWN.
END.

DELETE PROCEDURE hFuncProc.
3. Run this variant h-ConvTemp2.p, to get the same result as before. This time here's the end of the display, just to confirm that you got the arithmetic right:
An externally defined function such as this one can also reside in an entirely different OpenEdge session, connected to the procedure that uses the function using the OpenEdge AppServer. In this case, you declare the function in the same way but use the AppServer-specific ON SERVER phrase on the RUN statement to invoke the function. As with any AppServer call, you can't pass a buffer as a parameter to such a function. See OpenEdge Application Server: Developing AppServer Applications for more information.