6.2.3 Subroutine Subprograms: SUBROUTINE Statement

A subroutine subprogram is a program unit consisting of a SUBROUTINE statement followed by a series of statements that define a computing procedure. The CALL statement transfers control to a subroutine subprogram; a RETURN or END statement returns control to the calling program unit.

SUBROUTINE statements take the following form:

SUBROUTINE sub [([p [,p] . . . ])]
sub
Is the symbolic name of the subroutine.
p
Is a dummy argument. An asterisk (*) in the argument list specifies a dummy argument as an alternate return argument.

When control transfers to the subroutine, the values of any actual arguments in the CALL statement are associated with any corresponding dummy arguments in the SUBROUTINE statement. The statements in the subprogram are then executed.

The SUBROUTINE statement must be the first statement of a subroutine, unless an OPTIONS statement is specified.

A subroutine subprogram cannot contain a FUNCTION statement, a BLOCK DATA statement, a PROGRAM statement, or another SUBROUTINE statement. ENTRY statements are allowed to specify multiple entry points in the subroutine.

Examples

The first example contains a subroutine that computes the volume of a regular polyhedron, given the number of faces and the length of one edge. It uses the computed GO TO statement to determine whether the polyhedron is a tetrahedron, cube, octahedron, dodecahedron, or icosahedron. The GO TO statement also transfers control to the proper procedure for calculating the volume. If the number of faces is not 4, 6, 8, 12, or 20, the subroutine sends an error message to the terminal.

Main Program
COMMON NFACES, EDGE, VOLUME
ACCEPT *, NFACES, EDGE
CALL PLYVOL
TYPE *, 'VOLUME=', VOLUME
STOP
END

Subroutine
      SUBROUTINE PLYVOL
      COMMON NFACES, EDGE, VOLUME
      CUBED = EDGE**3
      GO TO (6,6,6,1,6,2,6,3,6,6,6,4,6,6,6,6,6,6,6,5), NFACES
      GO TO 6
1     VOLUME = CUBED * 0.11785
      RETURN
2     VOLUME = CUBED
      RETURN
3     VOLUME = CUBED * 0.47140
      RETURN
4     VOLUME = CUBED * 7.66312
      RETURN
5     VOLUME = CUBED * 2.18170
      RETURN
6     TYPE 100, NFACES
100   FORMAT (' NO REGULAR POLYHEDRON HAS ',I3,'FACES.'/)
      VOLUME = 0.0
      RETURN
      END

The next example uses alternate return specifiers to determine where control transfers on completion of the subroutine. The SUBROUTINE statement argument list contains two dummy alternate return arguments corresponding to the actual arguments *10 and *20 in the CALL statement argument list.

The decision about which RETURN statement executes depends on the value of Z, as computed in the subroutine:

Main Program                          Subroutine
    CALL CHECK(A,B,*10,*20,C)             SUBROUTINE CHECK(X,Y,*,*,Q)
    TYPE *, 'VALUE LESS THAN ZERO'        ...
    GO TO 30                          50  IF (Z)  60,70,80
10  TYPE*, 'VALUE EQUALS ZERO'        60  RETURN
    GO TO 30                          70  RETURN 1
20  TYPE*, 'VALUE MORE THAN ZERO'     80  RETURN 2
30  CONTINUE                              END
    ...

For More Information:


Previous Page Next Page Table of Contents