Try OpenEdge Now
skip to main content
ABL Essentials
Defining Functions : Defining a function
 

Defining a function

This is the syntax you use to define the header for a function:

Syntax

FUNCTION function-name[ RETURNS ]datatype[ ( parameters ) ] :
A function always must explicitly return a value of the data type you name. It can take one or more parameters just as a procedure can. Although this means that a function can return OUTPUT parameters or use INPUT-OUTPUT parameters just as a procedure can, you should consider that a function normally takes one or more INPUT parameters, processes their values, and returns its RETURN value as a result. If you find yourself defining a function that has OUTPUT parameters, you should reconsider and probably make it a procedure instead. One of the major features and benefits of a function is that you can embed it in a larger expression and treat its return value just as you would a variable of the same type. If there are OUTPUT parameters, this won't be the case as you must check their values in separate statements.
The body of the function can contain the same kinds of statements as an internal procedure. Just as with internal procedures, you cannot define a temp-table or any shared object within the function. It has an additional restriction, as well: you cannot reference a user-defined function within an ABL ASSIGN statement or other ABL updating statement that could cause an index to be updated. The interpreter might not be able to deal with transaction-related code inside the function itself in the middle of the update statement. It is a very good practice to avoid using functions in any code statements that update the database.
A function must contain at least one RETURN statement to return a value of the appropriate data type:
RETURN return-value.
The return-value can be a constant, variable, field, or expression (including possibly even another function reference) that evaluates to the data type the function was defined to return.
A function must end with an END statement. Just as an internal procedure normally ends with an END PROCEDURE statement, it is normal to end a function with an explicit END FUNCTION statement to keep the organization of your procedure file clearer. The FUNCTION keyword is optional in this case but definitely good programming practice.
Although a function must always have a return type and return a value, a reference to the function is free to ignore the return value. That is, an ABL statement can simply consist of a function reference without a return value, much like a RUN statement without the RUN keyword. This might be appropriate if the function returns a TRUE/FALSE value indicating success or failure, and in a particular piece of code, you are not concerned with whether it succeeded or not.
Whether a function takes any parameters or not, you must include parentheses, even if they're empty, on every function reference in your executable code.
* Making a forward declaration for a function
* Making run-time references with DYNAMIC-FUNCTION