In this topic, we described about the CANCEL Statement with detailed example.
The CANCEL statement ensures the specified subprogram is entered in initial state for the next time call.
literal-1 must be an alphanumeric literal. identifier-1 must be an alphanumeric, alphabetic or zoned decimal data item.
literal-1 or identifier-1 have its value is program-name. literal-1 or identifier-1 contents must be the same as a literal or an identifier contents specified in CALL statement.
After a CANCEL statement for a called subprogram has been executed, that subprogram no longer has a logical connection to the program. The contents of data items described by the subprogram are not changed when the subprogram is cancelled. The subprogram is entered in its initial state when a CALL statement naming the same subprogram is executed later by any program in the run unit.
When a CANCEL statement is executed, all programs contained within the program referenced in the CANCEL statement are also cancelled. The result is the same as if a valid CANCEL were executed for each contained program in the reverse order in which the programs appear in the separately compiled program.
A CANCEL statement closes all open files that are associated with the program named in an explicit CANCEL statement.
Called subprograms can contain CANCEL statements. However, a called subprogram must not execute a CANCEL statement that directly or indirectly cancels the calling program itself or that cancels any program higher than itself in the calling hierarchy. In such a case, the run unit is terminated.
A program specified in a CANCEL statement must has been called program and has executed an EXIT PROGRAM statement or a GOBACK statement.