Once a module is written, its global entities (i.e., global PARAMETERs, global variables and internal functions) can be made available to other modules and programs. The program or module that wants to use a particular module must have a USE statement at its very beginning. The USE statement has one of the following forms:
USE module-name USE module-name, ONLY: name-1, name-2, ..., name-n
Here is the meaning of these USEs:
Once a USE is specified in a program or in a module, every global entities of that used module (i.e., PARAMETERs, variables, and internal functions) becomes available to this program or this module. For example, if module SomeConstants is:PROGRAM MainProgram USE SomeConstants IMPLICIT NONE .......... END PROGRAM MainProgram
Then, program MainProgram can access to PARAMETERs PI and g and variable Counter.MODULE SomeConstants IMPLICIT NONE REAL, PARAMETER :: PI = 3.1415926 REAL, PARAMETER :: g = 980 INTEGER :: Counter END MODULE SomeConstants
Thus, MainProgram can use PI and Counter of module SomeConstants; but, MainProgram cannot use g!PROGRAM MainProgram USE SomeConstants, ONLY: PI, Counter IMPLICIT NONE .......... END PROGRAM MainProgram
USE with ONLY: is very handy, because it could only "import" those important and vital information and "ignore" those un-wanted ones.
For example, if one wants to give a new name to Counter of module SomeConstants, one should write:new-name => name-in-module
Thus, in a program, whenever it uses MyCounter, this program is actually using Counter of module SomeConstants.NewCounter => Counter
This kind of renaming can be used with ONLY: or without ONLY:.
The above example wants to use PI of module SomeConstants. In this case, when program MainProgram uses PI, it actually uses the PI of module SomeConstants. Program MainProgram also uses Counter of module SomeConstants; but, in this case, since there is a renaming, when MainProgram uses MyCounter it actually uses Counter of module SomeConstants.PROGRAM MainProgram USE SomeConstants, ONLY: PI, MyCounter => Counter IMPLICIT NONE .......... END PROGRAM MainProgram
Renaming does not require ONLY:. In the following, MainProgram can use all contents of module SomeConstants. When MainProgram uses PI and g, it uses the PI and g of module SomeConstants; however, when MainProgram uses MyCounter, it actually uses Counter of module SomeConstants.
Why do we need renaming? It is simple. In your program, you may have a variable whose name is identical to an entity of a module that is being USEed. In this case, renaming the variable name would clear the ambiguity.PROGRAM MainProgram USE SomeConstants, MyCounter => Counter IMPLICIT NONE .......... END PROGRAM MainProgram
In the above example, since MainProgram has a variable called g, which is the same as PARAMETER g in module SomeConstants. By renaming g of SomeConstants, MainProgram can use variable g for the variable and GravityConstant for the PARAMETER g in module SomeConstants.PROGRAM MainProgram USE SomeConstants, GravityConstant => g IMPLICIT NONE INTEGER :: e, f, g .......... END PROGRAM MainProgram
However, renaming is not a recommended feature. You should avoid using it whenever possible.