Subprograms
From CometWiki
Subprograms
A subprogram is a fully functional object program that can be called at any time from any Internet Basic program. Subprograms are written in Internet Basic and are completely self-contained.
Subprograms may or may not use the video windows feature of Comet. (For example, the Comet Help System uses subprograms to display help messages, run lookup programs, or run table lookup programs.)
Subprograms are called with the ENTER statement. When an ENTER statement is executed, the partition environment of the current program is saved and the subprogram is loaded and executed in the same partition.
The partition environment is passed in full to the called subprogram. This includes the data division (COMMON and LOCAL fields) and the open logical unit numbers. If the data division of the subprogram is structured exactly the same as the calling program, data values are preserved when the subprogram is executed. Data values are not preserved, however, when the subprogram is concluded.
The called subprogram must conclude with an EXIT, EXITALL, or EXITTO statement. When an EXIT statement is executed by the subprogram, the partition environment is restored to its previous state and the calling program continues executing at the next instruction following the ENTER statement. In this regard, the ENTER and EXIT statements are very similar to the GOSUB and RETURN subroutine statements, respectively.
When Comet restores the partition environment, it does so using the data saved when the ENTER statement was executed. All data values, formats, and instructions are restored to their original (pre-subprogram) values. Thus, data is not automatically passed back to the calling program (although, refer to the ENTER statement documentation for a programming method that can be used to pass data back to the calling program).
Subprograms may be nested up to 99 levels. Each ENTER statement calls another level of subprogram, and each EXIT statement returns to the previous level. The EXITALL statement returns to the original calling program (i.e., the top level). The EXITTO statement returns to a specified subprogram level.
Making hybrid programs
A Comet object program can be a "program" and "subprogram" at the same time.
By definition, a "program" is one that was started via the RUN statement. A "subprogram," on the other hand, is one that was started via the ENTER statement.
Any given program can determine it's own current status by interrogating the system variable named ENTERLEVEL.
Use the following code to make a hybrid program/subprogram:
IF ENTERLEVEL = 0 ! Was the program RUN? STOP ! Exit to QMONITOR ELSE ! Otherwise, the program was ENTERed EXIT ! Exit to calling program ENDIF