STE 58762
- -
2-23
When calling the subprogram from the main program, write (in the main program) the name of the
subprogram and the data you wish to pass over to that subprogram. For example, the
corresponding subprogram will have the statement:
PROGRAM SUBEXAMPLE (M1, M2, M3)
The subprogram SUBEXAMPLE will now do whatever it does while treating A as M1, B as M2, and
C as M3.
Note that variables changed in the subprogram will automatically change the corresponding value in
the main program. For example, if M3 were to change in the subprogram SUBEXAMPLE, C will
also change simultaneously in the main program.
Example:
Main program
PROGRAM MAIN
REMARK *** SAMPLE 2 ***
K1 = 15
K2 = 28
SUB2(K1, K2, K)
PRINT K
END
Sub program
PROGRAM SUB2(N1, N2, N3)
REMARK *** SUBPROGRAM NO. 2 ****
N3 = N1 + N2
RETURN
END
In the above example, three arguments are being passed off between the main program and
subprogram. Specifically, K1 of the main program is passed over as N1 of the subprogram.
Similarly, K2 of the main program is passed over as N2 of the subprogram. The subprogram adds
N1 and N2, and puts the result in a variable called N3. When this happens, the value of K in the
main program also changes (since K and N3 correspond to each other).