The CALL statement transfers control from a main program to an external subroutine.
Format
CALL name (expr1, expr2, expr3, ...) CALL @ var (expr1, expr2, expr3, ...) |
Parameter(s)
name |
Name of the subroutine to be called. |
expr… |
Values to be passed to the cataloged subroutine. If one of the values is an array variable, it must be preceded by the MAT keyword. |
@ var |
var is a variable which has been assigned the item-ID of the cataloged subroutine to be entered. |
Description
The CALL statement may be used to enter an external subroutine. An external subroutine is a subroutine that is compiled and cataloged separately from the programs that call it. When the ending RETURN statement of the subroutine is encountered, program control is returned to the original program at the line following the CALL statement.
The subroutine to which the CALL statement branches must be cataloged, unless the subroutine is in the same file as the main program. The first line of the subroutine must contain the SUBROUTINE statement. Control is returned to the main program when a RETURN is encountered in the subroutine which does not correspond to a previous GOSUB within the same external subroutine. If there is no RETURN statement, control does not return to the main program.
Each of the parameters listed in the CALL syntax line is passed into the corresponding variable list on the SUBROUTINE syntax line. Other than their positions on the CALL and SUBROUTINE syntax lines, there is no correspondence between variable names in the calling program and subroutine.
An alternative way of passing variables between programs and subroutines is by using COMMON statements in both program and subroutine. See COMMON Statement for more information.
The DATA statement may be used to supply input that the subroutine might request. See DATA Statement for more information.
When arrays are being passed from the main program to a subroutine, the array name must be preceded by the MAT keyword and there must be a one-to-one correspondence to the elements being passed. For example, to pass the 3x4 matrix MATRIX, type:
CALL SUBR( MAT MATRIX ) |
The MATRIX array must be previously dimensioned in the program with the DIM (dimension) statement.
In the subroutine SUBR, the corresponding dimensioned array must also be dimensioned. Note, however, that the corresponding arrays do not need to have the same dimensions, as long as they have the same number of elements. The first 2 lines of the subroutine SUBR might read:
SUBROUTINE SUBR( MAT ARRAY1) DIM ARRAY1(6,2) |
If the 3x4 matrix MATRIX in the main program contains:
1 |
2 |
3 |
4 |
RED |
BLUE |
GREEN |
YELLOW |
A |
B |
C |
D |
then when it is passed to ARRAY1, the 6x2 matrix contains:
1 |
2 |
3 |
4 |
RED |
BLUE |
GREEN |
YELLOW |
A |
B |
C |
D |
Examples
To call the subroutine ADDTHEM, passing variables A, B, and C:, the calling line in the main program would read:
CALL ADDTHEM(A, B, C ) |
The first line of the source code for ADDTHEM might then read:
SUBROUTINE ADDTHEM(X, Y, Z ) |
Variable A is passed to variable X, B is passed to Y, and C is passed to Z. When the subroutine has finished, these values are passed in the opposite direction.
See Also