Interface Blocks

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:

f90 prog4.f90 area.f90 InputOutput.f90
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 -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.

Syntax

The syntax of an interface block is the following:
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

  1. the function header that contains the types of the function value, function name, and arguments,
  2. the declaration of each argument, and
  3. the END FUNCTION statement.
Note that only the types of argument and their INTENT are important. You can use other names for the arguments; but, it would be a bad practice.

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:

INTEGER 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
The corresponding interface block (if both functions are used) is
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
If only function Volume() is used, one can just copy the information of Volume() to interface block as follows:
INTERFACE
   REAL FUNCTION  Volume(a, b, c)
      REAL, INTENT(IN) :: a, b, c
   END FUNCTION  Volume
END INTERFACE

Where Does the Interface Block Go?

The answer is simple. Put it after the IMPLICIT NONE statement. Thus, if the main program needs to use external functions Coin() and Volume(), the following shows the place for the interface block:
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