PROCEDURE Declaration [ SPL to HP C/XL Migration Guide ] MPE/iX 5.0 Documentation
SPL to HP C/XL Migration Guide
PROCEDURE Declaration
Table 8-2. PROCEDURE Declaration
------------------------------------------------------------------------------------------------
| | |
| SPL | HP C/XL Equivalent |
| | |
------------------------------------------------------------------------------------------------
| | |
| procedure-declaration: | function-definition: |
| | |
| 1. [type] PROCEDURE procedure-id | 1. [static] [type |
| | void] function-id |
| ( formal-parm [,...] ) ; | |
| | ( formal-parm [,...] ) |
| [VALUE formal-parm [,...] ;] | |
| | formal-parm-decl [;...] ; |
| formal-parm-decl [;...] ; | |
| | function-body |
| [OPTION option [,...] ;] | |
| | 2. [static] [type |
| [procedure-body ;] | void] function-id ( ) |
| | |
| 2. [type] PROCEDURE procedure-id | function-body |
| | |
| [OPTION option [,...] ;] | 3. extern [type |
| | void] function-id ( ) |
| [procedure-body ;] | |
| | |
| 3a. [type] PROCEDURE procedure-id | |
| | |
| OPTION EXTERNAL [, option] [...] ; | |
| | |
| 3b. [type] PROCEDURE procedure-id | |
| | |
| OPTION FORWARD [, option] [...] ; | |
| | |
------------------------------------------------------------------------------------------------
| | |
| formal-parm-decl: | formal-parm-decl: |
| | |
| a. type formal-parm [,...] | a. [type] formal-parm [,...] |
| | |
| b. [type] ARRAY formal-parm [,...] | b. [type] {formal-parm "[" "]"} [,...] |
| | |
| c. [type] POINTER formal-parm [,...] | c. [type] {* formal-parm} [,...] |
| | |
| d. [type] PROCEDURE formal-parm [,...] | d. [type] {formal-parm ( )} [,...] |
| | |
| e. LABEL formal-parm [,...] | e. (labels cannot be passed) |
| | |
------------------------------------------------------------------------------------------------
| | |
| option: | storage: |
| The CHECK, EXTERNAL, FORWARD, INTERNAL, | The extern and static storage classes are |
| INTERRUPT, PRIVILEGED, SPLIT, UNCALLABLE, | discussed in "Options" below. |
| and VARIABLE options are discussed in | |
| "Options" below. | |
| | |
------------------------------------------------------------------------------------------------
| | |
| procedure-body: | function-body: |
| | |
| a. statement | a. "{" statement "}" |
| | |
| b. BEGIN | b. "{" |
| | |
| [local-data-declarations] | [local-declarations] |
| | |
| [external/intrinsic-declarations] | [statement [...] ] |
| | |
| [local-subroutine-declarations] | "}" |
| | |
| [statement [;...] ] | |
| | |
| END | |
| | |
------------------------------------------------------------------------------------------------
| |
| Continued |
| |
------------------------------------------------------------------------------------------------
Table 8-2. PROCEDURE Declaration(cont.)
---------------------------------------------------------------------------------------------
| | |
| SPL | HP C/XL Equivalent |
| | |
---------------------------------------------------------------------------------------------
| | |
| A procedure declaration: | A function definition: |
| | |
| * defines a procedure identifier | * defines a function identifier |
| * specifies whether the procedure will | * specifies whether the function will NOT |
| return a value (type) | return a value (void) |
| * describes the parameters: number, | * describes the parameters: number, type |
| type, pass-by-value or | (all are pass-by-value) |
| pass-by-reference | * specifies a storage class |
| * specifies any options | * declares local variables |
| * declares local variables | * includes the statements of the function |
| * includes the statements of the | body |
| procedure body | |
| | |
---------------------------------------------------------------------------------------------
| | |
| Procedure declarations cannot be nested, | Same as SPL, using the extern storage |
| except that a procedure with OPTION | class. |
| EXTERNAL and no body may be declared in a | |
| procedure's local declarations. | |
| | |
---------------------------------------------------------------------------------------------
Data Type
Table 8-3. Data Type
---------------------------------------------------------------------------------------------
| | |
| SPL | HP C/XL Equivalent |
| | |
---------------------------------------------------------------------------------------------
| | |
| Default type: None | Default type: int (= long int) |
| | |
---------------------------------------------------------------------------------------------
| | |
| If type is specified, the procedure is a | Functions normally return a value and may |
| function procedure, which may be called in | be called in expressions. The value |
| an expression. The value returned is the | returned may be any type except array or |
| type specified. | another function. |
| | |
---------------------------------------------------------------------------------------------
| | |
| If type is omitted, the procedure does not | If the void type is specified, the function |
| return a value and cannot be used in | does not return a value and cannot be used |
| expressions. | in expressions. |
| | |
---------------------------------------------------------------------------------------------
| | |
| A value is returned by assigning it to the | A value is returned in a return statement: |
| procedure-id in the body of the procedure: | |
| | |
| | return expression ; |
| procedure-id := expression | |
| | For easier conversion, declare a local |
| For example: | variable, e.g., returnvalue, and replace |
| | the procedure-id with it in the function |
| INTEGER PROCEDURE FUNC ; | body. Then replace all the SPL RETURN |
| BEGIN | statements with "return returnvalue" Add |
| ... | one before the final "}": |
| FUNC := Y + Z ; | |
| ... | short int FUNC () ; |
| RETURN ; | { |
| ... | short int returnvalue ; |
| FUNC := A - B ; | ... |
| ... | returnvalue := Y + Z ; |
| END ; | ... |
| | return returnvalue ; |
| | ... |
| | returnvalue := A - B ; |
| | ... |
| | return returnvalue ; |
| | } |
| | |
---------------------------------------------------------------------------------------------
Parameters
Table 8-4. Parameters
---------------------------------------------------------------------------------------------
| | |
| SPL | HP C/XL Equivalent |
| | |
---------------------------------------------------------------------------------------------
| | |
| Formal parameters are defined by type (in | Formal parameters are defined by type (in |
| the formal-parm-decl section) and by | the formal-parm-decl section). All are |
| whether they are pass-by-reference (the | pass-by-value. |
| default) or pass-by-value (named in the | |
| VALUE section). | |
| | |
---------------------------------------------------------------------------------------------
| | |
| Simple variable and pointer formal | Simple variable formal parameters expect a |
| parameters may be pass-by-value or | value. Array, function, and pointer formal |
| pass-by-reference. Array, procedure, and | parameters expect a pointer value. |
| label formal parameters are | Consequently, the operation for arrays and |
| pass-by-reference only. | functions is functionally equivalent to SPL |
| | pass-by-reference. The operation for |
| "Reference" formal parameters expect the | simple variables and pointers is |
| address of the actual parameter. "Value" | functionally equivalent to SPL |
| formal parameters expect the value of the | pass-by-value. |
| actual parameter. | |
| | (Labels cannot be passed.) |
| | |
---------------------------------------------------------------------------------------------
| | |
| At the procedure call, if the formal | At the function call, the actual parameters |
| parameter is pass-by-value, the value of | are evaluated and converted to standard |
| the actual parameter is passed. | types. (See also "HP C/XL Rules for |
| | Automatic Numeric Type Conversion".) |
| If the formal parameter is | |
| pass-by-reference and the actual parameter | * [unsigned] char and short int become |
| is an appropriate identifier, array | [unsigned] int (= [unsigned] long int) |
| reference or pointer reference, the address | * float becomes double |
| of the actual parameter is passed; if the | * array identifiers become "pointers to |
| actual parameter is a constant or | array of type T" |
| expression, then its value is passed as the | * function identifiers become "pointers |
| address. | to function returning type T" |
| | * pointers are unchanged |
| It is possible to pass addresses in SPL as | * structures are unchanged (they are |
| type LOGICAL or INTEGER parameters. Such | copied into the function space) |
| operations must be examined carefully to | |
| determine the function performed in the | The conversions are based on the actual |
| original SPL code. | parameters, not on the corresponding formal |
| | parameters. The formal parameters "expect" |
| | the converted forms and reconvert them |
| | accordingly. HP C/XL does not check |
| | parameters. |
| | |
---------------------------------------------------------------------------------------------
| | |
| Addresses are 16-bit pointers. | Addresses are 32-bit pointers. |
| | |
---------------------------------------------------------------------------------------------
The HP C/XL equivalent of a formal "reference" simple variable or pointer
parameter is a pointer to simple variable or pointer to pointer,
respectively. This amounts (mostly) to the addition of a leading "*"
dereference operator everywhere the formal parameter is used in the
function, in the form "*formal-parm".
The HP C/XL equivalent of an actual "reference" simple variable or
pointer parameter is the address of the simple variable or pointer,
respectively. The address is obtained with the "&" address operator, in
the form "&actual-parm".
Since array-ids (no subscript) are passed as pointers, they are
implicitly pass-by-reference. That is, they may be passed as actual
parameters and used as formal parameters without the "&" and "*"
operators. If an array cell is passed by reference to an array or
pointer formal parameter, it requires the "&" operator, as in
"&array-id[cell]".
Options
Table 8-5. Options
---------------------------------------------------------------------------------------------
| | |
| SPL | HP C/XL Equivalent |
| | |
---------------------------------------------------------------------------------------------
| | |
| CHECK level | No equivalent. |
| | |
| Specifies varying degrees of parameter | HP C/XL performs no parameter checking. |
| checking for an external procedure. | |
| | |
---------------------------------------------------------------------------------------------
| | |
| EXTERNAL (Table 8-2, format 3a) | extern storage class (Table 8-2, format 3) |
| | |
| Defines the name, type, and parameters of a | The formal-parm list is omitted because HP |
| procedure which exists external to the | C/XL performs no parameter checking on |
| current program. | functions. |
| | |
---------------------------------------------------------------------------------------------
| | |
| FORWARD (Table 8-2, format 3b) | extern storage class (Table 8-2, format 3) |
| | |
| Specifies that the procedure will be | The function may be declared elsewhere in |
| declared fully later in the program. | the same compilation unit or in a separate |
| Allows a procedure to be called prior to | unit. If a function is not declared before |
| its declaration. | it is called, its type defaults to |
| | "function returning int". |
| | |
---------------------------------------------------------------------------------------------
| | |
| INTERNAL | No direct equivalent. |
| | |
| Prevents the procedure from being called | The static storage class provides similar |
| from another segment. Generally used to | functionality. A static function-id will |
| keep the procedure-id local. | not be exported to the linker, and |
| | therefore will be unknown to other |
| | compilation units. |
| | |
---------------------------------------------------------------------------------------------
| | |
| INTERRUPT | No equivalent. |
| | |
| Specifies an external interrupt procedure. | |
| The purpose is highly hardware-dependent. | |
| | |
---------------------------------------------------------------------------------------------
| | |
| PRIVILEGED | No equivalent. |
| | |
| Allows the procedure to execute in | |
| privileged mode. | |
| | |
---------------------------------------------------------------------------------------------
| | |
| SPLIT | No equivalent. |
| | |
| Aids privileged users running in | |
| split-stack mode. | |
| | |
---------------------------------------------------------------------------------------------
| | |
| UNCALLABLE | No equivalent. |
| | |
| Prevents the procedure from being called by | |
| code not executing in privileged mode. | |
| | |
---------------------------------------------------------------------------------------------
| | |
| VARIABLE | No direct equivalent. |
| | |
| Lets the procedure be called with a varying | The HP C/XL library header file varargs.h |
| number of actual parameters. The mechanism | contains macros that allow you to write |
| for determining how many actual parameters | functions with varying actual parameters. |
| are passed uses Q-register addressing. | Insert the file in your program with the |
| | directive: |
| | |
| | #include <varargs.h> |
| | |
| | See the HP C/XL Library Reference Manual |
| | for details. |
| | |
---------------------------------------------------------------------------------------------
MPE/iX 5.0 Documentation