OOP Syntax Elements

FUNCTION Directive

 

FUNCTION [PERFORM] [LOCAL] method (args) logic [FUNCTION END]

A method (function) is declared via the FUNCTION directive within the DEF CLASS .. END DEF block. Every method declaration needs to have associated logic that will be called when it is invoked. If arguments (args) are used, then the type and number must match parameters that the application code provides. Parentheses are part of the method name (whether or not arguments are used).

PERFORM indicates that the logic is to be loaded and executed (as in a PERFORM directive); therefore, all variables will be shared with the calling program. While this does have some uses, it clearly violates the rules of Encapsulation. LOCAL indicates that the method is only to be called internally from within the object. It cannot be called externally.

Statement Label or In-Line Logic

A statement label is normally used to access logic defined as a procedure outside the DEF CLASS .. END DEF block.

Example:

0060 FUNCTION Find(X$) LookupCust
...
0120 END DEF
...
0210 LookupCust:
0220 ENTER Cst_id$
...                 ! Logic to find the client
0240 RETURN sts     ! Return value indicates success

However, the logic can also appear directly following the FUNCTION directive.

Example:

0060 FUNCTION Find(X$)
0070 ENTER Cst_id$
...                 ! Logic to find the client
0090 RETURN sts      ! Return value indicates success
0100 FUNCTION END
...
0170 END DEF

In which case, the method declaration should close with a FUNCTION END clause. However, the logic ends automatically with the start of the next method declaration or END DEF (whichever comes first).

Return Value

Each method should return a value. The value can take the form of a string or numeric value, depending on the name associated to the method (strings must end with $).

If no RETURN value is specified, then the system will return a value of zero for numeric methods and " " (null) for string methods.

Naming Conventions

Multiple definitions of the same method name can be specified, as long as each method has different args. To determine which method to actually use, PxPlus attempts to match up the arguments specified with the lists provided in the application.

Example:

In the Class Definition:

FUNCTION Find(X$) LookupByName 
FUNCTION Find(X) LookupByNumber 
... ... ...
LookupByName:
ENTER Cst_id$
... ... ... ! Logic to find the client by name
RETURN ...
LookupByNumber:
ENTER Cst_id 
... ... ... ! Logic to find the client by number
RETURN ... 

In the Application: 

Cst'Find("ABCD")      This calls LookupByName
Cst'Find(1234)          This calls LookupByNumber

See Also

FUNCTION Directive
PERFORM Directive