HP 3000 Manuals

SUBROUTINE Declaration [ SPL to HP C/XL Migration Guide ] MPE/iX 5.0 Documentation


SPL to HP C/XL Migration Guide

SUBROUTINE Declaration 

          Table 8-26.  SUBROUTINE Declaration 

-----------------------------------------------------------------------------------------------
|                                             |                                               |
|                     SPL                     |              HP C/XL Equivalent               |
|                                             |                                               |
-----------------------------------------------------------------------------------------------
|                                             |                                               |
| subroutine-declaration:                     | Global.  function-definition:                 |
|                                             |                                               |
|      1. [type] SUBROUTINE subroutine-id     |      1. static [type                          |
|                                             |                   void] function-id           |
|             ( formal-parm [,...] ) ;        |                                               |
|                                             |             ( formal-parm [,...] )            |
|             [VALUE formal-parm [,...] ]     |                                               |
|                                             |             formal-parm-decl [;...] ;         |
|             formal-parm-decl [;...] ;       |                                               |
|                                             |             "{" statement "}"                 |
|             statement ;                     |                                               |
|                                             |      2. static [type                          |
|      2. [type] SUBROUTINE subroutine-id     |                   void] function-id ( )       |
|                                             |                                               |
|             statement ;                     |             "{" statement "}"                 |
|                                             |                                               |
|                                             |                                               |
| statement                                   | Local.  define-directive:                     |
|   may be any SPL statement, including       |                                               |
|   compound (BEGIN-END).                     |      1. #define function-id(                  |
|                                             |                                               |
|                                             |             formal-parm [,...] )              |
|                                             |                                               |
|                                             |             statement-process                 |
|                                             |                                               |
|                                             |          :                                    |
|                                             |                                               |
|                                             |          #undef function-id                   |
|                                             |                                               |
|                                             |      2. #define function-id statement-process |
|                                             |                                               |
|                                             |          :                                    |
|                                             |                                               |
|                                             |          #undef function-id                   |
|                                             |                                               |
-----------------------------------------------------------------------------------------------
|                                             |                                               |
| A subroutine is like a procedure that has   | No direct equivalent.                         |
| no option or local declaration sections.    |                                               |
|                                             |                                               |
-----------------------------------------------------------------------------------------------
|                                             |                                               |
| Declared at the global level, it is         | A static function is the closest global       |
| available only to the main body of the      | equivalent.  It is available to all           |
| compilation unit.  It may access global     | functions in the compilation unit.  It may    |
| identifiers.                                | access global identifiers.                    |
|                                             |                                               |
-----------------------------------------------------------------------------------------------
|                                             |                                               |
| Declared at the local level, it is          | The "best" local equivalent is a #define      |
| available only to the procedure body where  | macro directive, which will be expanded       |
| it is declared.  It may access global and   | inline wherever it is called.  See "DEFINE    |
| local identifiers.                          | Declaration and Reference" for the #define    |
|                                             | syntax rules.  Note that there is no          |
|                                             | control whatsoever over the data types of     |
|                                             | the macro's formal and actual parameters.     |
|                                             |                                               |
|                                             | The alternate solution is a static function   |
|                                             | at the global level.  This can be awkward     |
|                                             | because the local procedure variables that    |
|                                             | were known to the subroutine are no longer    |
|                                             | automatically available.  You could change    |
|                                             | the local variables to global, or pass them   |
|                                             | as parameters to the new function.            |
|                                             |                                               |
-----------------------------------------------------------------------------------------------

          Table 8-27.  SUBROUTINE Declaration Example 

-----------------------------------------------------------------------------------------------------
|                                 |                               |                                 |
|         SPL Subroutine          |       HP C/XL Function        |    HP C/XL define Directive     |
|                                 |                               |                                 |
-----------------------------------------------------------------------------------------------------
|                                 |                               |                                 |
|                                 |                               |                                 |
|      INTEGER SUBROUTINE A(B,C); |      static short int A(B,C); |      #define A(B,C) ( (B)+(C) ) |
|      VALUE B,C;                 |      short int B,C;           |      ...                        |
|      INTEGER B,C;               |      {                        |      #undef A                   |
|      A := B+C;                  |      return B+C;              |                                 |
|                                 |      }                        |                                 |
|                                 |                               |                                 |
-----------------------------------------------------------------------------------------------------
|                                                                                                   |
| This example shows the conversion of an SPL subroutine to an HP C/XL function and a               |
| #define macro directive.                                                                          |
|                                                                                                   |
| In the #define directive version, the parentheses around B and C and the summation                |
| are necessary to ensure correct evaluation of the parameters when the substitutions               |
| for B and C are expressions.                                                                      |
|                                                                                                   |
-----------------------------------------------------------------------------------------------------

Careful examination of an SPL procedure may reveal that local variables
have been declared in the procedure for the sole purpose of providing
them to the subroutine.  In that case, the variable declarations may
simply be moved to the new function.

It is permitted (but rarely used) to execute a GOTO statement from an SPL
subroutine to a label within the body of the enclosing procedure.  HP
C/XL restricts the goto statement to labels within the same function
declaration.



MPE/iX 5.0 Documentation