Function

Mon 01 January 2018

Syntax

Function name[(paramlist)] [Returns data_type]
   [statements]
End-Function

Where paramlist is:

&param1 [As data_type] [, &param2 [As data_type]]... 

Where data_type is any valid data type, including Number, String, Date, Rowset, SQL, Record, and so on.

Where statements is a list of PeopleCode statements.

Description

PeopleCode functions can be defined in any PeopleCode program. Function definitions must be placed at the top of the program, along with any variable and external function declarations.

Functions can be called from the program in which they are defined, in which case they don’t need to be declared, and they can be called from another program, in which case they need to be declared at the top of the program where they are called.

Any variables declared within a function are valid for the scope of the function.

By convention, external PeopleCode functions are stored in records whose names begin in FUNCLIB_, and they are always placed in the FieldFormula event (which is convenient because this event should no longer be used for anything else).

Note: Functions can be stored in the FieldFormula event only for record fields, not for component record fields.

A function definition consists of:

  • The keyword Function followed by the name of the function and an optional list of parameters. The name of the function can be up to 100 characters in length.

  • An optional Returns clause specifying the data type of the value returned by the function.

  • The statements to be executed when the function is called.

  • The End-function keyword.

The parameter list, which must be enclosed in parentheses, is a comma-separated list of variable names, each prefixed with the & character. Each parameter is optionally followed by the keyword As and the name for one of the conventional PeopleCode data types (Number, String, Date, and so on) or any of the object data types (such as Rowset, SQL, and so on.) If you specify data types for parameters, then function calls are checked to ensure that values passed to the function are of the appropriate type. If data types are not specified, then the parameters, like other temporary variables in PeopleCode, take on the type of the value that is passed to them.

Note: If a parameter is listed in the function definition, then it is required when the function is called.

PeopleCode parameters are always passed by reference. This means that if you pass the function a variable from the calling routine and change the value of the variable within the function, the value of the variable is changed when the flow of execution returns to the calling routine.

If the function is to return a result to the caller, the optional Returns part must be included to specify the data type of the returned value. You have seven choices of value types: Number, String, Date, Time, DateTime, Boolean, or Any.

PeopleCode internal subroutines are part of the enclosing program and can access the same set of variables as the other statement-lists of the program, in addition to local variables created by the parameters and local variable declarations within the function.