Developer Guide and Reference

ID 767251
Date 10/31/2024
Public
Document Table of Contents

Internal Procedures

Internal procedures are functions or subroutines that are defined in the internal-subprogram-part of a main program, a subroutine or function program unit, or a module procedure in a program unit. The program unit or module procedure in which the internal procedure appears is called its host.

An internal procedure takes the following form:

CONTAINS

internal-subprogram

[internal-subprogram] ...

internal-subprogram

Is a function or subroutine subprogram that defines the procedure. An internal subprogram must not contain any other internal subprograms.

Description

Internal procedures are the same as external procedures, except for the following:

  • An internal procedure can be called from its host, or it can be invoked through a procedure pointer or a dummy procedure outside its host.

    If it references a data object from its host, the object must have the SAVE attribute (implicitly or explicitly), be allocated if it is allocatable, and be associated if it is a pointer when the host program unit is not active in the call list.

  • An internal procedure has access to host entities by host association; that is, names declared in the host program unit are useable within the internal procedure.

  • An internal procedure must not contain an ENTRY statement.

  • The name of the internal procedure is not a global name.

An internal procedure can reference itself (directly or indirectly); it can be referenced in the execution part of its host and in the execution part of any internal procedure contained in the same host (including itself).

The interface of an internal procedure is always explicit.

Examples

The following example shows an internal procedure:

PROGRAM COLOR_GUIDE
...
 CONTAINS
  FUNCTION HUE(BLUE)   ! An internal procedure
  ...
  END FUNCTION HUE
END PROGRAM

The following example program contains an internal subroutine find, which performs calculations that the main program then prints. The variables a, b, and c declared in the host program are also known to the internal subroutine.

 program INTERNAL
 ! shows use of internal subroutine and CONTAINS statement
    real a,b,c
    call find
    print *, c
 contains
    subroutine find
     read *, a,b
     c = sqrt(a**2 + b**2)
    end subroutine find
 end