# Directives

FUNCTION | Declare Object Method

## Formats

1. Declare Method: | FUNCTION[ HIDE ] method [(args)] logic
2. Method with PERFORM Behavior: | FUNCTION [ HIDE ] PERFORM method [(args)] logic
3. Local Method: | FUNCTION [ HIDE ] LOCAL method [(args)] logic
4. COM Event Method: | FUNCTION method [(args)] logic FOR EVENT {event$ | SAME}
5. End Method Declaration: | FUNCTION END

Where:

(args) | Optional parameter list within parenthesis.
END | Keyword indicating the end of a method declaration.
event$ | Name of the corresponding COM event.
FOR EVENT | Keyword indicating that method is associated with given COM event.
HIDE | Keyword indicating that method is not to be displayed in the list of methods returned by '*.
LOCAL | Keyword indicating that method is only to be called within the object. #### Note: The 'IL' system parameter must be Off to enforce the LOCAL option.
logic | Procedure associated with method. Method should return a value; if not, the system forces 0 or " ".
method | Name of method that the object can perform. (In Object Oriented Programming, functions are referred to as methods.)
PERFORM | Keyword indicating that logic is to be loaded/executed as in a PERFORM.
SAME | Keyword to use if the method name matches event$ name.

## Description

The FUNCTION directive is used to declare a method for an object in Object Oriented Programming (OOP). Each method must have associated logic that will be called when it is invoked:

> function Find(X$)LookupCust
> LookupCust:

> enter Cst_id$
> ! Logic to find the client
> return sts ! Return value indicates success

Alternatively, the function logic can directly follow the FUNCTION declaration:

> function Find(X$)
> enter Cst_id$
> ! Logic to find the client
> return sts ! Return value indicates success

The declaration ends with a FUNCTION END directive for the method itself, by the start of the next method declaration, or when END DEF is reached at the end of the object definition:

> def class "MyObj"
> function MyFirstMethod()
> a=b,d=f
> if ... ... ... etc.
> function MySecondMethod()
> a=b,d=f
> if ... ... ... etc.
> function end
> end def

Every method should return a value. The value can take the form of a string or numeric value depending on the name associated with the function (string functions must end with $). If no RETURN value is specified, then the system will return a value of zero for numeric functions and "" (null) for string functions.

#### Note:
As a general rule of thumb, methods should return a non-zero value when successfully executed. This allows for logic such as:
if not(Cst'Find("ABCD")) \
then gotoBad_cust

When arguments are used in the definition, then the type/number should normally match variables in the application code. Multiple definitions of the same method name can be specified, as long as each method has a different parameter list. In order to determine which method to actually use, PxPlus attempts to match up the parameter lists specified with the variables provided in the application.

If an * (asterisk) is used in the definition in place of the argument list (e.g. FUNCTION readbykey(*)), the method will be invoked regardless of the type/number of variables to be matched in the corresponding logic.

FUNCTION PERFORM indicates that the function logic is to be loaded and executed (as in a PERFORM directive). All variables will be shared with the calling program.

#### Note:
The PERFORM format violates the general rules of OOP encapsulation.

FUNCTION LOCAL indicates that the function is only to be called internally from within the object. It cannot be called externally.

(The FUNCTION HIDE directive was added in PxPlus v8.11.)

## See Also

Object Oriented Programming
DEF CLASS Define Object Class
ON EVENT Event Processing

## Example

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