Progress
Programming
Handbook
Locally Defined User-defined Functions
The following procedure defines doubler(), then references it three times:
Defining User-defined Functions
The FUNCTION statement defines doubler(), a user-defined function that accepts an integer, multiplies it by two, and returns the result.
The FUNCTION statement specifies the name of the function (doubler), the data type the function returns (INTEGER), and the following information on the single parameter: its mode (INPUT), its name within the definition (parm1), and its data type (INTEGER).
The function’s logic resides in the RETURN statement—specifically, in the expression that follows the RETURN keyword. The logic is so simple (taking a number and multiplying it by two) that it does not need separate statements. If the logic were more complex and needed separate statements, they would reside between the FUNCTION statement and the RETURN statement.
The END FUNCTION statement ends the block that the FUNCTION statement begins. If you omit END FUNCTION and run the procedure, nothing appears, because Progress includes the DISPLAY statement within the user-defined function, which is probably not what you want. Progress Software Corporation recommends that when you define a user-defined function, you conclude the block with the END FUNCTION statement.
Referencing User-defined Functions
The DISPLAY statement references doubler() three times:
The previous example defines the user-defined function by using the FUNCTION statement. Here is its syntax:
For function–name, substitute the name of the function. For data–type, substitute the data type the function returns. For param, which stands for parameter, substitute the following syntax:
The preceding syntax shows three ways of specifying a parameter, which correspond to the three types of parameter that user-defined functions allow: scalar (elementary data item), temporary table, and database table buffer. In the top third of the syntax diagram, which describes scalar parameters, note how each parameter has a mode (INPUT, OUTPUT, or INPUT–OUTPUT), a name, and a data type.
For more information on the FUNCTION statement, see its reference entry in the Progress Language Reference .
Referencing User-defined Functions Before Defining Them
Progress lets a procedure reference a user-defined function before defining it, as long as the procedure declares (preannounces) the user-defined function first. In other words, a procedure can declare, reference, and define a user-defined function, in that order. The declaration describes the user-defined function just enough to enable the Progress compiler, which reads procedure files from top to bottom, to resolve references to it.
NOTE: These declarations are sometimes called forward declarations.To declare a user-defined function, code a FUNCTION statement that includes:
Here are some additional notes on declaring user-defined functions:
The following procedure declares, references, and defines the user-defined function doubler(). Note that the references occur before the definition:
Copyright © 2004 Progress Software Corporation www.progress.com Voice: (781) 280-4000 Fax: (781) 280-4095 |