INCLUDE

Statement: Directs the compiler to stop reading statements from the current file and read statements in an included file or text module.

The INCLUDE statement takes one of the following forms:

Syntax

INCLUDE 'filename [/[NO]LIST]'
INCLUDE '[text-lib] (module-name) [/[NO]LIST]' (VMS only)

filename
Is a character string specifying the name of the file to be included; it must not be a named constant.
The form of the file name must be acceptable to the operating system, as described in your system documentation.

/[NO]LIST
Specifies whether the incorporated code is to appear in the compilation source listing. In the listing, a number precedes each incorporated statement. The number indicates the "include" nesting depth of the code. The default is /NOLIST. /LIST and /NOLIST must be spelled completely.

On Windows, Tru64 UNIX, and Linux systems, you can only use /[NO]LIST if you specify the /vms compiler option (which sets OpenVMS defaults).

text-lib (VMS only)
Is a character string specifying the file name of the text library to be searched.
The form of the file name must be acceptable to the operating system, as described in your system documentation.


module-name (VMS only)
Is a character string specifying the name of the text library module to be included. The name of the text module must be enclosed in parentheses. It can be up to 31 characters long and can contain any alphanumeric character and the special characters dollar sign ($) and underscore ( _ ).

Rules and Behavior

An INCLUDE statement can appear anywhere within a scoping unit. The statement can span more than one source line, but no other statement can appear on the same line. The source line cannot be labeled.

An included file or text module cannot begin with a continuation line, and each Fortran statement must be completely contained within a single file.

An included file or text module can contain any source text, but it cannot begin or end with an incomplete Fortran statement.

The included statements, when combined with the other statements in the compilation, must satisfy the statement-ordering restrictions shown in Statements.

Included files or text modules can contain additional INCLUDE statements, but they must not be recursive. INCLUDE statements can be nested until system resources are exhausted.

When the included file or text module completes execution, compilation resumes with the statement following the INCLUDE statement.

You can use modules instead of include files to achieve encapsulation of related data types and procedures. For example, one module can contain derived type definitions as well as special operators and procedures that apply to those types. For information on how to use modules, see Program Units and Procedures.

Compatibility

CONSOLE STANDARD GRAPHICS QUICKWIN GRAPHICS WINDOWS DLL LIB

See Also: MODULE, USE

Examples

In the following example, a file named COMMON.FOR (in the current working directory) is included and read as input.

Including Text from a File

Main Program File                COMMON.FOR File

PROGRAM
  INCLUDE 'COMMON.FOR'           INTEGER, PARAMETER :: M=100
  REAL, DIMENSION(M) :: Z        REAL, DIMENSION(M) :: X, Y
  CALL CUBE                      COMMON X, Y
  DO I = 1, M
    Z(I) = X(I) + SQRT(Y(I))
    ...
  END DO
END

SUBROUTINE CUBE
  INCLUDE 'COMMON.FOR'
  DO I=1,M
    X(I) = Y(I)**3
  END DO
  RETURN
END

The file COMMON.FOR defines a named constant M, and defines arrays X and Y as part of blank common.

The following example program declares its common data in an include file. The contents of the file INCLUDE.INC are inserted in the source code in place of every INCLUDE 'INCLUDE.INC' statement. This guarantees that all references to common storage variables are consistent.

INTEGER i
REAL x
INCLUDE 'INCLUDE.INC'

DO i = 1, 5
  READ (*, '(F10.5)') x
  CALL Push (x)
END DO