Functions

Syntax

FUNCTION identifier ( [parameter-list] ) RETURN mx-datatype 
IS
    block	
parameter-list :=
    parameter-spec parameter-specs

parameter-spec :=
    identifier [parameter-mode] mx-datatype

parameter-specs :=
    [ , parameter-spec parameter-specs ]

parameter-mode :=
      IN
    | IN OUT
    | OUT
Where,
mx-datatype

Specifies a PL/MX datatype as described in Datatypes that is also a NonStop SQL/MX datatype. The datatypes are INT, INTEGER, LARGEINT, and SMALLINT.

parameter-mode

Specifies the direction of dataflow through the argument. IN specifies that the dataflow is only into the function. OUT specifies that the dataflow is only out of the function. IN OUT specifies that the dataflow is bidirectional. The UDFs invoked directly by a NonStop SQL/MX query must have only IN parameters. However, PL/MX procedures used in a CALL statement may have any combination of IN, OUT, and IN OUT parameters.

Example

Declare a function that calculates the factorial of an integer:
FUNCTION factorial( n in INTEGER ) RETURN INTEGER
IS
  fact     INTEGER;
  inner_n  INTEGER;
 BEGIN
  fact  := 1;
  FOR inner_n IN REVERSE 2..n LOOP
    fact := fact * inner_n;
  END LOOP;
		RETURN fact;
 END factorial;