MODULEs

Global Data Access

Example

  MODULE state_variables
    INTEGER, PARAMETER      :: NX=100, NY=200
    REAL, DIMENSION(NX, NY) :: temp, pres
  END MODULE state_variables

  PROGRAM
    USE state_variables

    DO IY = 1, NY
        DO IY = 1, NY
            CALL update_temp(IX, IY)
            CALL update_pres(IX, IY)
        END DO
    END DO
  END PROGRAM

  ! -- external subprogram :
  SUBROUTINE update_temp(I,J)
    USE state_variables
    INTEGER, INTENT(IN) :: I, J

    temp(I,J) = ...
  END SUBROUTINE

Module Procedures

Example

  MODULE related_procedures

    PUBLIC  :: sub_1
    PRIVATE :: sub_2, sub_3

    CONTAINS

    SUBROUTINE sub_1(A,B)
      REAL, INTENT(IN)    :: A(:)
      INTEGER, INTENT(IN) :: B
      ...
      CALL sub_2(B)
      CALL sub_3(B)
      ...
    END SUBROUTINE sub_1

    SUBROUTINE sub_2(C)
      REAL, INTENT(IN) :: C(:,:)
      ...
    END SUBROUTINE sub_2

    SUBROUTINE sub_3(D)
      INTEGER, INTENT(IN) :: D
      ...
    END SUBROUTINE sub_3
  END MODULE related_procedures

Type and Procedure Packaging (Encapsulation)

Example

Encapsulation of a derived TYPE and its associated subprogrammes yields the beginnings of object-oriented programming :
  MODULE Houses
      TYPE House
          PRIVATE                      ! -- components of House can be 
                                       !    accessed by MODULE subprogrammes
                                       !    only;
          CHARACTER(LEN=32) :: owner
          INTEGER           :: residents
          REAL              :: value
      END TYPE House

      CONTAINS
          SUBROUTINE set_value(H, val)
              TYPE(House), INTENT(INOUT) :: H
              REAL, INTENT(IN)           :: val

              H%value = val
          END SUBROUTINE 

          FUNCTION value(H)
              TYPE(House), INTENT(IN) :: H
              REAL                    :: value

              value = H%value
          END FUNCTION
  END MODULE Houses

The USE Statement

Usually simply
  USE module_name
This makes all PUBLIC entities defined within module_name accessible.

Name clashes can occur --- one remaining maintainability problem in Fortran? Workaround: --- the local name Set_House_Value references the module-procedure (declared within House) Set_Value :
  USE House, Set_House_Value => Set_Value
(In Fortran 77 the problem is insoluble.)

This form reduces the chance of a name-clash and is a "safer" option from a software-engineering standpoint :
  USE My_Module, ONLY: Sub_1, Sub_2

Remark

Making interfaces explicit (INTERFACE)


...previousup (conts)next...



About this document:

Produced from the SGML: /home/isd/public_html/_course_fortran_90/_reml_grp/index.reml
On: 10/2/2003 at 10:33:52
Options: reml2 -i noindex -l long -o html -p multiple