All functions you have seen so far are internal functions that are contained in a program or a module. Functions that are not contained in any program or modules are external functions. A program can use internal functions, external functions and functions in modules. Moreover, external functions can be in the same file of the program or in several files.
External functions can be considered as program units that are independent of each other. Thus, the only way of communication among external functions, the main program and modules is through arguments. In other words, from outside of an external function, it is impossible to use its variables, PARAMETERs and internal functions.
For example, your main program could be in file prog4.f90. Your functions Area() and Test() are in file area.f90 and functions ReadData() and DisplayResult() are in file InputOutput.f90. In this case, all four functions are external to the main program. To compile this three-file program, you need the following command:
This will generate a a.out. Or, you can ask the Fortran compiler to generate an executable called prog4 with the following command:f90 prog4.f90 area.f90 InputOutput.f90
f90 prog4.f90 area.f90 InputOutput.f90 -o prog4
In addition to compiling these files, there is one more important thing. How does a function or a program know the way of using a function? A function has a name, some arguments with certain types, and the type of the function value? Without these information, a function might not be used correctly. To overcome this problem, an interface block is introduced. More precisely, any external function to be used should be listed in an interface block along with the declaration of its arguments and their types and the type of the function value.
Note that an external function can be in the file containing the main program or module. As long as that function is not contained in any program, function, or module, it is external and an interface block is required in any program, function or module where this function is used.
INTERFACE type FUNCTION name(arg-1, arg-2, ..., arg-n) type, INTENT(IN) :: arg-1 type, INTENT(IN) :: arg-2 .......... type, INTENT(IN) :: arg-n END FUNCTION name ....... other functions ....... END INTERFACE
An interface block starts with the keyword INTERFACE and ends with END INTERFACE. For each external function to be used in a program, module or a function, it should have an entry in an interface block. The information that should be included are
In fact, you can copy these information from that function's declarations to an interface block. For example, if there are two external functions in a file as follows:
The corresponding interface block (if both functions are used) isINTEGER FUNCTION Coin(value) IMPLICIT NONE INTEGER, INTENT(IN) :: value ........... END FUNCTION Coin REAL FUNCTION Volume(a, b, c) IMPLICIT NONE REAL, INTENT(IN) :: a, b, c ........... END FUNCTION Volume
If only function Volume() is used, one can just copy the information of Volume() to interface block as follows:INTERFACE INTEGER FUNCTION Coin(value) INTEGER, INTENT(IN) :: value END FUNCTION Coin REAL FUNCTION Volume(a, b, c) REAL, INTENT(IN) :: a, b, c END FUNCTION Volume END INTERFACE
INTERFACE REAL FUNCTION Volume(a, b, c) REAL, INTENT(IN) :: a, b, c END FUNCTION Volume END INTERFACE
PROGRAM CoinVolume IMPLICIT NONE INTERFACE INTEGER FUNCTION Coin(value) INTEGER, INTENT(IN) :: value END FUNCTION Coin REAL FUNCTION Volume(a, b, c) REAL, INTENT(IN) :: a, b, c END FUNCTION Volume END INTERFACE ..... other specification statements ..... ......... executable statements .......... END PROGRAM CoinVolume