PROC

A procedure is a script fragment that can be called throughout the Master Template. Create a procedure when you need to use the same piece of code in more than one place.

The following is an example of the procedure declaration.

PROC proc_name ( parameter_1; parameter_2; ..., optional parameter list ) 
DO 
			<Template scripting part with declarations> 
OD 

The requirements for declaring a procedure are as follows:

  • Procedures must be declared before they are used.
  • The name of the PROC must start with a lowercase character and is limited to lowercase characters, digits, and underscores. Spaces are not allowed.
  • The parameter list is optional.

The requirements for the parameter list are the following:

  • Parameters must be separated with a semicolon.
  • A parameter can be one of four types: CONST TYPE name (a parameter cannot be changed in the PROC); TYPE name (a variable as parameter); ARRAY TYPE name (an array as parameter); MAP TYPE name (a map as parameter).
  • The parameter name (parameter_1) must start with a lowercase character and is limited to lowercase characters, digits, and underscores. Spaces are not allowed.
  • TYPE must be used to indicate the type pf parameter. The following types can be used: TEXT, NUMBER, BOOL, and FIELDSET. TYPE can also be a previously defined DATASTRUCTURE. For more information on the types of parameters, see Types.

    Examples are provided here.

    PROC myproc1(TEXT par; CONST TEXT cpar; ARRAY TEXT apar; MAP TEXT mpar)
    DO
    OD
    

    PROC myproc2(BOOL par; CONST BOOL cpar; ARRAY BOOL apar; MAP BOOL mpar)
    DO
    OD
    

    PROC myproc3(NUMBER par ; CONST NUMBER cpar; ARRAY NUMBER apar; MAP NUMBER mpar)
    DO
    OD
    

    DATASTRUCTURE MyStruct
    BEGIN
    END
    

    PROC myproc4(FIELDSET FPar; MyStruct spar)
    DO
    OD
    

CONST parameters values cannot be changed in the procedure. The values of variable and ARRAY parameters can be changed in the procedure.

ARRAY and MAP cannot be passed as CONST parameters.

FIELDSET and Data Structures cannot be used as an ARRAY or MAP parameter or passed as a CONST parameter.

You cannot use recursive procedures.

The following is an example of a procedure call.

APROC proc_name ( parameter_1; parameter_2; ..., the actual parameter list) 

The number of parameters and their types in the actual parameter list must be the same as the parameter list in the declaration of the procedure.

The following is an example of the definition.


PROC amount2 ( CONST NUMBER money ; TEXT result)
DO 
    NUMBER positive  :=  money
    IF money < 0 
        THEN 
        ASSIGN positive  :=  - money
    FI 
    ASSIGN result  := number( positive ; 2)
    IF money = 0 
        THEN 
        ASSIGN result  :=  "-.-"

        ELIF money < 0 
            THEN 
            ASSIGN result  :=  result + " negative"
    FI 
OD  

Use the procedure as follows.

TEXT amount_text 
APROC amount2 ( 23.14 ; amount_text) 
# 
€ @( amount_text) 
# 
APROC amount2 ( 0.0 ; amount_text) 
# 
€ @( amount_text) 
# 

In this example, 23.14 is assigned to money and the value of amount_text is assigned to result.