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 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 |
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 |
Usually simply
USE module_name |
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 |
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 |
module.variable |
module.procedure |
...previous | up (conts) | next... |