next up previous contents
Next: Functions Up: Program Units Previous: Procedures

 

Subroutines

Consider the following example,

      PROGRAM Thingy
       IMPLICIT NONE
       .....
        CALL OutputFigures(Numbers)
       .....
      CONTAINS
       SUBROUTINE OutputFigures(Numbers)
        REAL, DIMENSION(:), INTENT(IN) :: Numbers
         PRINT*, "Here are the figures", Numbers
       END SUBROUTINE OutputFigures
      END PROGRAM Thingy

The subroutine here simply prints out its argument. Internal procedures can `see' all variables declared in the main program (and the IMPLICIT NONE statement). If an internal procedure declares a variable which has the same name as a variable from the main program then this supersedes the variable from the outer scope for the length of the procedure.

Using a procedure here allows the output format to be changed easily. To alter the format of all outputs, it is only necessary to change on line within the procedure.

Internal subroutines lie between CONTAINS and END PROGRAM statements and have the following syntax

 
 SUBROUTINE < procname >[ (< dummy args >) ]

< declaration of dummy args >

< declaration of local objects >

...

< executable stmts >

END [ SUBROUTINE [< procname > ] ]

(Recall that not all HPF compilers implement Internal subroutines.)

A SUBROUTINE may include calls to other procedures either from the same main program, from an attached module or from an external file. Note how, in the same way as a main program, a SUBROUTINE must terminate with an END statement. It is good practise to to append SUBROUTINE and the name of the routine to this line as well.

Fortran 90 also allows recursive procedures (procedure that call themselves). In order to promote optimisation a recursive procedure must be specified as such -- it must have the RECURSIVE keyword at the beginning of the subroutine declaration (see Section 14.6).

Now try this question gif

Return to corresponding overview page gif


next up previous contents
Next: Functions Up: Program Units Previous: Procedures

©University of Liverpool, 1997
Wed May 28 20:20:27 BST 1997
Not for commercial use.