LF Fortran 95

CONTAINS Statement

Description

The CONTAINS statement separates the body of a main program, module, or subprogram from any internal or module subprograms it contains.

Syntax

CONTAINS

Remarks

When used in a MODULE, the CONTAINS statement separates a global data area from any module procedures. Any variables, type definitions, interfaces or initialization expressions that appear above the CONTAINS statement are globally available within the module, and are also available to any program unit that uses the module, provided that the entity in question has the PUBLIC attribute.

When appearing in a main program, subprogram or module procedure, the CONTAINS statement separates the main body of code from any internal procedures. Any variables, type definitions, interfaces or initialization expressions that appear above the CONTAINS statement are available to all internal procedures that appear below the CONTAINS statement.

Any variables, type definitions, interfaces, or initialization expressions that appear below a CONTAINS statement are local in scope.

The CONTAINS statement is not executable.

Internal procedures cannot contain other internal procedures.

Example

module mod1 real :: a=1. ! a is globally available contains ! separates global from procedures subroutine sub1() real :: b=3. ! b is only available inside sub1() write(*,*) a ! global a is available inside sub1() call internal() contains ! separates sub1 body from internal proc subroutine internal() ! internal() is local to sub1 real :: a=2. ! this a is local to internal() write(*,*) b ! b is available by host association write(*,*) a ! global a is not available because ! it is overridden by local a end subroutine internal end subroutine sub1 end module mod1   program prog1 use mod1 call internal() contains subroutine internal() write(*,*) a ! global a is available by host association call sub1() end subroutine internal end program