SUBROUTINE

Purpose

The SUBROUTINE statement is the first statement of a subroutine subprogram.

Syntax

Read syntax diagramSkip visual syntax diagram   .------------.
   V            |
>>---+--------+-+--SUBROUTINE--name----------------------------->
     '-prefix-'
 
>--+-------------------------------+---------------------------->
   '-(--+---------------------+--)-'
        '-dummy_argument_list-'
 
>--+------------------------------------------------+----------><
   '-BIND--(--C--+-----------------------------+--)-'
                 '-,--NAME-- = --binding_label-'
 

prefix
is one of the following:
  • FORTRAN 95 Begins ELEMENTAL FORTRAN 95 Ends
  • FORTRAN 95 Begins PURE FORTRAN 95 Ends
  • RECURSIVE
    Note:
    type_spec is not permitted as a prefix in a subroutine.
name
is the name of the subroutine subprogram
Fortran 2003 Standard
binding_label
a scalar character initialization expression
End of Fortran 2003 Standard

Rules

At most one of each kind of prefix can be specified.

The subroutine name cannot appear in any other statement in the scope of the subroutine, unless recursion has been specified.

The RECURSIVE keyword must be specified if, directly or indirectly,

If the RECURSIVE keyword is specified, the procedure interface is explicit within the subprogram.

Fortran 95

Using the PURE or ELEMENTAL prefix indicates that the subroutine may be invoked by the compiler in any order as it is free of side effects.For elemental procedures, the keyword ELEMENTAL must be specified. If the ELEMENTAL keyword is specified, the RECURSIVE keyword cannot be specified.

End of Fortran 95
IBM Extension

You can also call external procedures recursively when you specify the -qrecur compiler option, although XL Fortran disregards this option if the SUBROUTINE statement specifies the RECURSIVE keyword.

End of IBM Extension
Fortran 2003 Standard

The BIND keyword implicitly or explicitly defines a binding label by which a procedure is accessed from the C programming language. A dummy argument cannot be zero-sized. A dummy argument for a procedure with the BIND attribute must have interoperable types and type parameters, and cannot have the ALLOCATABLE, OPTIONAL, or POINTER attribute.

The BIND attribute must not be specified for an internal procedure. If the SUBROUTINE statement appears as part of an interface body that describes a dummy procedure, the NAME= specifier must not appear. An elemental procedure cannot have the BIND attribute.

End of Fortran 2003 Standard

Examples

RECURSIVE SUBROUTINE SUB(X,Y)
  INTEGER X,Y
  IF (X.LT.Y) THEN
    RETURN
  ELSE
    CALL SUB(X,Y+1)
  END IF
END SUBROUTINE SUB

Related information