What Is A Module?

What is a Module?

In many previous example, there are several internal functions packed at the end of the main program. In fact, many of them such as Factorial(), Combinatorial(), GCD(), and Prime() are general functions that can also be used in other programs. To provide the programmers with a way of packing commonly used functions into a few tool-boxes, Fortran 90 has a new capability called modules. It has a syntactic form very similar to a main program, except for something that are specific to modules.

Syntax

The following is the syntax of a module:
MODULE module-name IMPLICIT NONE [specification part] CONTAINS [internal-functions] END MODULE module-name

The differences between a program and a module are the following:

  • The structure of a module is almost identical to the structure of a program.
  • However, a module starts with the keyword MODULE and ends with END MODULE rather than PROGRAM and END PROGRAM.
  • A module has specification part and could contain internal function; but, it does not have any statements between the specification part and the keyword CONTAINS.
  • Consequently, a module does not contains statements to be executed as in a program. A module can only contain declarations and functions to be used by other modules and programs. This is perhaps one of the most important difference. As a result, a module cannot exist alone; it must be used with other modules and a main program

Short Examples

Here are some short examples of modules:
  • The following is a very simple module. It has the specification part and does not have any internal function. The specification part has two REAL PARAMETERs, namely PI and g and one INTEGER variable Counter.
    MODULE SomeConstants IMPLICIT NONE REAL, PARAMETER :: PI = 3.1415926 REAL, PARAMETER :: g = 980 INTEGER :: Counter END MODULE SomeConstants
  • The following module SumAverage does not have any specification part (hence no IMPLICIT NONE); but it does contain two functions, Sum() and Average(). Please note that function Average() uses Sum() to compute the sum of three REAL numbers.
    MODULE SumAverage CONTAINS REAL FUNCTION Sum(a, b, c) IMPLICIT NONE REAL, INTENT(IN) :: a, b, c Sum = a + b + c END FUNCTION Sum REAL FUNCTION Average(a, b, c) IMPLICIT NONE REAL, INTENT(IN) :: a, b, c Average = Sum(a,b,c)/3.0 END FUNCTION Average END MODULE SumAverage
  • The following module DegreeRadianConversion contains two PARAMETERs, PI and Degree180, and two functions DegreeToRadian() and RadianToDegree(). The two PARAMETERs are used in the conversion functions. Note that these parameters are global to the two functions. See scope rules for the details.
    MODULE DegreeRadianConversion IMPLICIT NONE REAL, PARAMETER :: PI = 3.1415926 REAL, PARAMETER :: Degree180 = 180.0 REAL FUNCTION DegreeToRadian(Degree) IMPLICIT NONE REAL, INTENT(IN) :: Degree DegreeToRadian = Degree*PI/Degree180 END FUNCTION DegreeToRadian REAL FUNCTION RadianToDegree(radian) IMPLICIT NONE REAL, INTENT(IN) :: Radian RadianToDegree = Radian*Degree180/PI END FUNCTION RadianToDegree END MODULE DegreeRadianConversion

Tag » What Is A Module In Programming