Part 8. Modules

(c) 2017 by Barton Paul Levenson



If you have a Fortran program with a contains statement, and functions and subroutines after that, anything declared in the main program is visible to the subprograms. They have "global scope," as opposed to the "local scope" in a function or subroutine. So there seems to be no need for one new feature of Fortran: modules.

A module is like a program, but without an executable main program section. The syntax is:


module moduleName
    declarations (constants, types, variables, etc.)

contains

    subprograms, if any
end module moduleName


You could then use anything in the module with a use statement near the top of a program or subprogram:


    use moduleName


But why bother?

Well, if you have a useful bunch of constants, or useful types you created, or useful subroutines that are pretty generic, you can have more than one program use them this way. You don't have to duplicate code. It's basically a way to share data and code between programs, and it can be very useful for that.

If you're a physical scientist, you could create a "constants.mod" with all the NIST CODATA math and physics constants in it. If you're doing statistics, you could have generic routines to find descriptive statistics of an array of numbers--mean, sample standard deviation, skewness, kurtosis, etc. Modules let you create your own software libraries.





Page created:05/05/2017
Last modified:  05/05/2017
Author:BPL