Developer Guide and Reference

ID 767251
Date 10/31/2024
Public
Document Table of Contents

Define Generic Names for Procedures

An interface block or a GENERIC statement can be used to specify a generic name to reference all of the procedures within the interface block.

The initial line for such an interface block takes the following form:

INTERFACE generic-name

generic-name

Is the generic name. It can be the same as any of the procedure names in the interface block, or the same as any accessible generic name (including a generic intrinsic name).

A generic name can be the same as a derived-type name. In this case, all of the procedures in the interface block must be functions.

A generic interface can be used to extend or redefine a generic intrinsic procedure.

The procedures that are given the generic name must be the same kind of subprogram: all must be functions, or all must be subroutines.

Any procedure reference involving a generic procedure name must be resolvable to one specific procedure; it must be unambiguous. For more information, see Unambiguous Generic Procedure References.

The following is an example of a procedure interface block defining a generic name:

INTERFACE GROUP_SUBS
  SUBROUTINE INTEGER_SUB (A, B)
    INTEGER, INTENT(INOUT) :: A, B
  END SUBROUTINE INTEGER_SUB

  SUBROUTINE REAL_SUB (A, B)
    REAL, INTENT(INOUT) :: A, B
  END SUBROUTINE REAL_SUB

  SUBROUTINE COMPLEX_SUB (A, B)
    COMPLEX, INTENT(INOUT) :: A, B
  END SUBROUTINE COMPLEX_SUB
END INTERFACE

The three subroutines can be referenced by their individual specific names or by the group name GROUP_SUBS.

The following example shows a reference to INTEGER_SUB:

INTEGER V1, V2
CALL GROUP_SUBS (V1, V2)

Consider the following:

 INTERFACE LINE_EQUATION

   SUBROUTINE REAL_LINE_EQ(X1,Y1,X2,Y2,M,B)
     REAL,INTENT(IN) :: X1,Y1,X2,Y2
     REAL,INTENT(OUT) :: M,B
   END SUBROUTINE REAL_LINE_EQ

   SUBROUTINE INT_LINE_EQ(X1,Y1,X2,Y2,M,B)
     INTEGER,INTENT(IN) :: X1,Y1,X2,Y2
     INTEGER,INTENT(OUT) :: M,B
   END SUBROUTINE INT_LINE_EQ

 END INTERFACE

In this example, LINE_EQUATION is the generic name which can be used for either REAL_LINE_EQ or INT_LINE_EQ. Fortran selects the appropriate subroutine according to the nature of the arguments passed to LINE_EQUATION. Even when a generic name exists, you can always invoke a procedure by its specific name. In the previous example, you can call REAL_LINE_EQ by its specific name (REAL_LINE_EQ), or its generic name LINE_EQUATION.

The following statement adds subroutine COMPLEX_LINE to the generic interface LINE_EQUATION. Note that there must be an accessible explicit interface for COMPLEX_LINE.

  GENERIC :: LINE_EQUATION => COMPLEX_LINE

See Also