Use and Host Association

Use association allows the entities in a module to be accessible to other scoping units. The mechanism for use association is the USE statement. The USE statement provides access to all public entities in the module, unless ONLY is specified. In this case, only the entities named in the ONLY list can be accessed.

Host association allows the entities in a host scoping unit to be accessible to an internal procedure, derived-type definition, or module procedure contained within the host. The accessed entities are known by the same name and have the same attributes as in the host. Entities that are local to a procedure are not accessible to its host.

Use or host association remains in effect throughout the execution of the executable program.

If an entity that is accessed by use association has the same nongeneric name as a host entity, the host entity is inaccessible. A name that appears in the scoping unit as an external name in an EXTERNAL statement is a global name, and any entity of the host that has this as its nongeneric name is inaccessible.

An interface body does not access named entities by host association, but it can access entities by use association.

If a procedure gains access to a pointer by host association, the association of the pointer with a target that is current at the time the procedure is invoked remains current within the procedure. This pointer association can be changed within the procedure. After execution of the procedure, the pointer association remains current, unless the execution caused the target to become undefined. If this occurs, the host associated pointer becomes undefined.


Note: Implicit declarations can cause problems for host association. It is recommended that you use IMPLICIT NONE in both the host and the contained procedure, and that you explicitly declare all entities.

When all entities are explicitly declared, local declarations override host declarations, and host declarations that are not overridden are available in the contained procedure.

Examples

The following example shows host and use association:

 MODULE SHARE_DATA
   REAL Y, Z
 END MODULE

 PROGRAM DEMO
   USE SHARE_DATA         ! All entities in SHARE_DATA are available
   REAL B, Q              !   through use association.
   ...
   CALL CONS (Y)
 CONTAINS
   SUBROUTINE CONS (Y)    ! Y is a local entity (dummy argument).
     REAL C, Y
     ...
     Y = B + C + Q + Z    ! B and Q are available through host association.
     ...                  !   C is a local entity, explicitly declared.  Z
   END SUBROUTINE CONS    !   is available through use association.
 END PROGRAM DEMO

The following example shows how a host and an internal procedure can use host-associated entities:

 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

In this example, the variables a, b, and c are available to the internal subroutine find through host association. They do not have to be passed as arguments to the internal procedure. In fact, if they are, they become local variables to the subroutine and hide the variables declared in the host program.

Conversely, the host program knows the value of c, when it returns from the internal subroutine that has defined c.

For More Information: