7 92 2 SUBROUTINE Comments Warnings

LANSA Technical

7.92.2 SUBROUTINE Comments / Warnings

The parameters passed to a subroutine by an EXECUTE command can be a field name, an alphanumeric literal, a numeric literal, a system variable or a process parameter.

The parameters defined in a subroutine (ie: in the PARMS parameter) must be field names. All fields used in the PARMS parameter must be defined in the LANSA data dictionary or within the function by a DEFINE command.

The values specified in the WITH_PARMS parameter of an EXECUTE command are mapped into the fields specified in the PARMS parameter of the associated SUBROUTINE command just prior to executing the subroutine when they have the attribute *BOTH or *RECEIVED.

When the subroutine has completed execution the fields nominated in the PARMS parameter of the SUBROUTINE command are conditionally mapped back into the values specified in the WITH_PARMS parameter of the EXECUTE command. The condition is that fields are not mapped back when the WITH_PARMS value is a literal value (alphanumeric or numeric), a system variable, a process parameter or the parameter has the attribute *RECEIVED.

The default mapping value for parameters is *BOTH. However, it is worth taking the time to specify *RECEIVED or *RETURNED as this will reduce the time taken for parameter mapping.

Subroutines can be coded anywhere within a function and even nested within one another like this:

SUBROUTINE NAME(SUB01)
..........
DISPLAY .. etc, etc
 
      SUBROUTINE NAME(SUB02)
      ..........
      ..........
      ..........
      ENDROUTINE (subroutine SUB02)
 
CHANGE .. etc,etc
..........
 
      SUBROUTINE NAME(SUB03)
      ..........
      ..........
      ..........
      ENDROUTINE (subroutine SUB03)
..........
GOTO .....
..........
..........
ENDROUTINE (subroutine SUB01)
 

However, most programmers prefer to program subroutines at the end of the function, without nesting them inside one another.

There are very slight performance benefits in coding subroutines in decreasing order of use. If the most heavily used subroutines are coded first, then the operating system will have less work to do when you request that a subroutine be executed.

In the example above the command "following" (ie: executed after) the DISPLAY command is the CHANGE command even though subroutine SUB02 is between the 2 commands. Positioning SUB02 this way does not cause it be executed. The only way to execute SUB02 is via an EXECUTE command.

  • The ability to code subroutines anywhere within a function and nest subroutines within one another does not imply any "scope" of the subroutines or the fields that they declare. The "scope" or "scoping" facility is a feature of some computer languages such as PL/1 and is not available in LANSA.