Intel® Fortran Compiler Classic and Intel® Fortran Compiler Developer Guide and Reference

ID 767251
Date 6/24/2024
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

SUBROUTINE

Statement: The initial statement of a subroutine subprogram. A subroutine subprogram is invoked in a CALL statement or by a defined assignment or operator, and does not return a particular value.

[prefix [prefix]] SUBROUTINE name [([d-arg-list]) [lang-binding]]

   [specification-part]

   [execution-part]

[CONTAINS

   [internal-subprogram-part] ]

END [SUBROUTINE [name]]

prefix

(Optional) Is any of the following:

  • ELEMENTAL

    Acts on one array element at a time. This is a restricted form of a procedure.

  • IMPURE

    Asserts that the procedure has side effects.

  • MODULE

    Indicates a separate module procedure. See separate module procedures.

  • NON_RECURSIVE

    Indicates a procedure is not recursive.

  • PURE

    Asserts that the procedure has no side effects.

  • RECURSIVE

    Permits direct or indirect recursion to occur.

At most one of each of the above can be specified. You cannot specify both NON_RECURSIVE and RECURSIVE. You cannot specify both PURE and IMPURE. You cannot specify ELEMENTAL if lang-binding is specified.

name

Is the name of the subroutine.

d-arg-list

(Optional) Is a list of one or more dummy arguments or alternate return specifiers (*) .

lang-binding

(Optional) Takes the following form:

BIND (C [, NAME=ext-name])

ext-name

Is a character scalar constant expression that can be used to construct the external name.

specification-part

Is one or more specification statements.

execution-part

Is one or more executable constructs or statements.

internal-subprogram-part

Is one or more internal subprograms (defining internal procedures). The internal-subprogram-part is preceded by a CONTAINS statement.

Description

A subroutine is invoked by a CALL statement or defined assignment. When a subroutine is invoked, dummy arguments (if present) become associated with the corresponding actual arguments specified in the call.

Execution begins with the first executable construct or statement following the SUBROUTINE statement. Control returns to the calling program unit once the END statement (or a RETURN statement) is executed.

A subroutine subprogram cannot contain a BLOCK DATA statement, a PROGRAM statement, a MODULE statement, or a SUBMODULE statement. A subroutine can contain SUBROUTINE and FUNCTION statements to define internal procedures. ENTRY statements can be included to provide multiple entry points to the subprogram.

You need an interface block for a subroutine when:

  • Calling arguments use argument keywords.

  • Some arguments are optional.

  • A dummy argument is an assumed-shape array, allocatable, a coarray, a pointer, or a target.

  • The subroutine extends intrinsic assignment.

  • The subroutine can be referenced by a generic name.

  • The subroutine is in a dynamic-link library.

If the subroutine is in a DLL and is called from your program, use the option DLLEXPORT or DLLIMPORT, which you can specify with the ATTRIBUTES directive.

Note that if you specify lang-binding, you have to use the parentheses even if there are no arguments. For example, without lang-binding you can specify SUBROUTINE F but with lang-binding you have to specify SUBROUTINE F( ) BIND (C).

Example

The following example shows a subroutine:

Main Program          Subroutine
CALL HELLO_WORLD      SUBROUTINE HELLO_WORLD
...                   PRINT *, "Hello World"
END                   END SUBROUTINE

The following example uses alternate return specifiers to determine where control transfers on completion of 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
    ...

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 value of Z determines the return, as follows:

  • If Z < zero, a normal return occurs and control is transferred to the first executable statement following CALL CHECK in the main program.

  • If Z = = zero, the return is to statement label 10 in the main program.

  • If Z > zero, the return is to statement label 20 in the main program.

Note that an alternate return is an obsolescent feature in the Fortran Standard.

The following shows another example:

     SUBROUTINE GetNum (num, unit)
     INTEGER num, unit
 10  READ (unit, '(I10)', ERR = 10) num
     END
    ...