Common blocks in Fortran 77 were the only portable means of achieving global access of data throughout a collection of subprograms. This is unsafe, error-prone, and encourages bad programming practices in general. Fortran 90 provides a new program unit, a module, that replaces the common block and also provides many other features that allow modularization and data hiding, key concepts in developing large, maintainable numerical code.
Modules consist of a set of declarations and module procedures that are grouped under a single global name available for access in any other program unit via the use statement. Interfaces to the contained module procedures are explicit and permit compile time type-checking in all program units that use the module. Visibility of items in a module may be restricted by using the private attribute. The public attribute is also available. Those identifiers not declared private in a module implicitly have the public attribute.
module TypicalModule
private swap ! Make swap visible only within this module.
contains
subroutine order( x, y ) ! Public by default.
integer, intent( inout ) :: x, y
if ( abs( x ) < abs( y ) ) call swap( x, y )
end subroutine order
subroutine swap( x, y )
integer, intent( inout ) :: x, y
integer tmp
tmp = x; x = y; y = tmp ! Swap x and y.
end subroutine swap
end module TypicalModule
program UseTypicalModule
use TypicalModule
! Declare and initialize x and y.
integer :: x = 10, y = 20
print *, x, y
call order( x, y )
print *, x, y
end program UseTypicalModule