Try OpenEdge Now
skip to main content
ABL Essentials
Defining Functions : User-defined functions
 

User-defined functions

As you have already seen in the previous chapter's discussion of INTERNAL-ENTRIES and the GET-SIGNATURE method, there is an alternative to the internal procedure as a named entry point within a procedure file. This is the user-defined function, sometimes referred to as a UDF or simply as a function. You are probably familiar with functions from other programming languages. Fundamentally, a function differs from a procedure in that it returns a value of a specific data type, and therefore can act in place of any other kind of variable or expression that would evaluate to the same data type. This means that you can place functions inside other expressions within a statement or within the WHERE clause of a result set definition. By contrast, you must invoke a procedure, whether external or internal, with a RUN statement. If it needs to return information, you have to use OUTPUT parameters that your code examines after the procedure returns. You should therefore consider defining a function within your application for a named entry point that needs to return a single value and that is convenient to use within larger expressions.
You can also compare user-defined functions with the many built-in functions that are a part of ABL, such as NUM-ENTRIES, ENTRY, and many others that you've seen and used. Your functions can provide the same kinds of widely useful behavior as the built-in functions, but specific to your application.
If you use a function in a WHERE clause or other expression in the header of a FOR EACH block, the ABL Virtual Machine (AVM) evaluates the function once, at the time the query with the WHERE clause is opened or the FOR EACH block is entered.
You can perhaps best think of a function as a small piece of code that performs a frequently needed calculation, or that checks a rule or does some common data transformation. In general, this is the best use for functions. However, a function can be a more complex body of code if this is appropriate.