COPY and GOSUB are almost identical in that they both bring the code inline
to your process. In fact, when you code something like:
GOSUB XXX SUBROUTINE
where SUBROUTINE is *not* a label in the current event point, then APPX will
create
LABEL SUBROUTINE
COPY XXX SUBROUTINE
RETURN
The major difference between COPY and GOSUB is that GOSUB has its own set of
T/F flags, whereas COPY just copies the requested code into the event point
at that location, and if there are any T/F flags involved, the copied routine
better know about them. Example:
READ <some file>
F COPY <routine a>
The code in routine <routine a> will have the F flag set in the first position
when it starts running, and any T/F flags it changes will be visible to the
code following the COPY. If the copy was a GOSUB instead, then in <routine
a>, the first T/F flag would not be set, and any T/F flags it changes would
*not* be visible to the statements following the GOSUB.
A SUBR is another process invocation, so:
- the code exists as a separate process, which is sometimes required.
For example, you can call SUBR within a BEG READ/END READ loop, and
the routine can also do a BEG READ/END READ on the same file without
any problems.
- you can control sharing via the SUBPROCESS/RELATED/DETACHED invocation
- the code is *not* compiled into the calling program, thus reducing EM
size (but who cares, really).
- you cannot exit the routine with a RETURN, you must have an END or
EXIT, or just run out of statements.
- takes longer to invoke, as a separate process needs to be brought
into memory and control passed to it, and then returned to the caller.
Not a problem for data entry programs but can add run time if it's
in a update & gets invoked hundreds of times.
Because of the difference in exiting a SUBR (with END/EXIT) vs. a GOSUB (with
RETURN), you can't really call the routine one way or the other. You must
decide if it's going to be invoked as SUBR or GOSUB, and code accordingly.
If a SUBR attempts to exit with RETURN, you will get a run time error, however,
if a GOSUB encounters an END/EXIT, then you have just ended the execution of
the entire Event Point, not just that subroutine. This is a subtle programming
error that can lead to difficult to track down bugs, ie, sometimes a post just
skips part of the updating for a record because someone put an END instead of
RETURN in a GOSUB'd routine.
As to which one to use:
- COPY if you just want to bring in some straightforward code, i.e., for
this current ORDER2 record, read the ORDER1, CUSTOMER and PRODUCT files.
- GOSUB if you need to preserve the T/F flags
- SUBR if you *need* the special advantages it offers.
|