The subroutine parameters specification can be included in the subroutine heading to declare parameters for the procedure. These parameters are assigned values taken from the statement that invoked the subroutine. The parameters are treated as local variables within the subroutine; they can be interrogated or assigned new values anywhere within the subroutine or within any subroutines nested within it.
Declarations in a subroutine cannot declare an identifier that has the same name as any of the parameters of that routine. Each parameter in the parameter list must be preceded by a keyword specifying the parameter type.
A parameter is considered to be call-by-reference unless it is followed by the word VALUE, which indicates that the parameter is call-by-value. Task and file variables can only be call-by-reference.
Note: | Any changes made to the value of a call-by-reference parameter in the course of the subroutine also change the value of the variable that was passed to that parameter. By contrast, any changes made to the value of a call-by-value parameter in the course of the subroutine do not affect the value of the variable that was passed to that parameter. |
If a constant or an expression is passed as a parameter in the subroutine invocation statement, that parameter is passed by value whether or not VALUE is specified for that parameter. When a real parameter is passed to an integer in a subroutine call, it requires integer truncation and treats the real parameter as an expression. Because an expression is passed as a parameter, that parameter is passed by value.
When a parameter is a string expression, the string expression can contain up to 1800 characters.
Example
The following example illustrates the use of subroutines:
?BEGIN JOB SUBSHOW; INTEGER VAL1 := 7, % Global variable declarations VAL2 := 11; % Begin subroutine declaration SUBROUTINE FIRSTSUB(INTEGER PARAM1, INTEGER PARAM2 VALUE); BEGIN PARAM1 := PARAM1 * 2; % Changes made to the values of the PARAM2 := PARAM2 * 2; % parameters END FIRSTSUB; % End subroutine declaration FIRSTSUB(VAL1,VAL2); % Subroutine invocation RUN (RAJA)AREA/MEASURE(VAL1,VAL2); % Task initiation ?END JOB.
In this example, the global variables VAL1 and VAL2 are assigned values of 7 and 11, respectively. The subroutine invocation statement passes the variables VAL1 and VAL2 as values for the parameters PARAM1 and PARAM2. PARAM1 is therefore assigned a value of 7, and PARAM2 is assigned a value of 11. The subroutine contains statements that double the values of these parameters to 14 and 22, respectively.
The RUN statement that follows the subroutine invocation statement passes VAL1 and VAL2 to the program (RAJA)AREA/MEASURE. The values the program receives are 14 and 11. This occurs because the change made to the value of the call-by-reference parameter PARAM1 also affects the global variable VAL1 that was passed to it. The change made to the call-by-value variable PARAM2 does not affect VAL2.