Procedure termination

A procedure is terminated when, by some means other than a procedure reference, control passes back to the invoking program, block, or to some other active block.

Procedures terminate normally under the following circumstances:

  • Control reaches a RETURN statement within the procedure. The execution of a RETURN statement returns control to the point of invocation in the invoking procedure. If the point of invocation is a CALL statement, execution in the invoking procedure resumes with the statement following the CALL. If the point of invocation is a function reference, execution of the statement containing the reference is resumed.
  • Control reaches the END statement of the procedure. Effectively, this is equivalent to the execution of a RETURN statement.

Procedures terminate abnormally under the following circumstances:

  • Control reaches a GO TO statement that transfers control out of the procedure. The GO TO statement can specify a label in a containing block, or it can specify a parameter that has been associated with a label argument passed to the procedure. A STOP statement is executed in the current thread of a single-threaded program or in any thread of a multithreaded program.
  • An EXIT statement is executed.
  • The ERROR condition is raised and there is no established ON-unit for ERROR or FINISH. Also, if one or both of the conditions has an established ON-unit, ON-unit exit is by normal return rather than by a GO TO statement.
  • The procedure calls or invokes another procedure that terminates abnormally.

Transferring control out of a procedure using a GO TO statement can sometimes result in the termination of several procedures and/or begin-blocks. Specifically, if the transfer point specified by the GO TO statement is contained in a block that did not directly activate the block being terminated, all intervening blocks in the activation sequence are terminated. Consider following example:

  A: procedure options(main);
     statement-1
     statement-2
     B: begin;
        statement-b1
        statement-b2
        call C;
        statement-b3
     end B;
     statement-3
     statement-4
     C: procedure;
        statement-c1
        statement-c2
        statement-c3
        D: begin;
           statement-d1
           statement-d2
           go to Lab;
           statement-d3
        end D;
        statement-c4
     end C;
     statement-5
  Lab: statement-6
     statement-7
  end A;

A activates B, which activates C, which activates D. In D, the statement go to Lab transfers control to statement-6 in A. Since this statement is not contained in D, C, or B, all three blocks are terminated; A remains active. Thus, the transfer of control out of D results in the termination of intervening blocks B and C as well as the termination of block D.