LF Fortran 95



An INTERFACE block specifies the forms of reference by which a procedure can be invoked. An interface block specifies a procedure interface, a defined operation, or a defined assignment.

An INTERFACE statement begins an interface block.

An END INTERFACE statement ends an interface block.


INTERFACE [generic-spec] interface-spec END INTERFACE [generic-spec]

generic-spec is the name of a generic procedure, or one of

OPERATOR (defined-operator) ASSIGNMENT (=)

defined-operator is one of the intrinsic operators or .operator-name.

operator-name is a user-defined name for the operation, consisting of one to 31 letters.

interface-spec specifies whether the procedure is a subroutine or a function, and any dummy arguments that the procedure might have. If the interface is a generic interface, interface operator or interface assignment, interface-spec may also specify that the procedure is a module procedure, as long as that procedure appears in a module that is within the scope of the procedure declaring the interface, or is available by use association.


Explicit interface

An interface block for a procedure consists of the characteristics of the procedure, the name of the procedure, and the name and characteristics of any dummy arguments.

A dummy argument name appearing in an interface specifies the keyword for that dummy argument.

Interface blocks cannot be specified for procedures that are contained within a MODULE, because explicit interfaces for module procedures are provided automatically.

Generic interface

An interface statement with a generic-name specifies a generic interface for each of the procedures in the interface block. Each procedure in the interface block may have an explicit interface, or may name a module procedure that is contained in the same module.

Each procedure's argument list within the generic interface must have a calling sequence that is unique, otherwise the interface is ambiguous, and an error will be produced at compile time.

The specific procedure is selected at runtime, based on the argument list used when the generic procedure is called.

If the interface is a generic, assignment or operator interface, if a generic-spec is present in the END INTERFACE statement, it must be identical to the generic spec in the INTERFACE statement.

If the interface is not a generic, assignment or operator interface, generic-spec cannot be present in the END INTERFACE statement.

Defined operations

If OPERATOR is specified in an INTERFACE statement, all of the procedures specified in the interface block must be functions that can be referenced as defined operations. In the case of binary operators, the function requires two arguments. In the case of unary operators, a function with one argument must be used.

OPERATOR must not be specified for functions with no arguments or for functions with more than two arguments.

The dummy arguments must be non-optional data objects and must be specified with INTENT((IN).

The function result must not have assumed CHARACTER length.

If the operator is an intrinsic-operator, the number of function arguments must be consistent with the intrinsic uses of that operator.

A given defined operator may, as with generic names, apply to more than one function, in which case it is generic in exact analogy to generic procedure names. For intrinsic operator symbols, the generic properties include the intrinsic operations they represent. Because both forms of each relational operator have the same interpretation, extending one form (such as <=) has the effect of defining both forms (<= and .LE.).

Overriding an intrinsic operator for an intrinsic data type is not allowed. Operators may only be defined for data types which do not already have a definition for that particular operator.

Defined assignments

If ASSIGNMENT is specified in an INTERFACE statement, all the procedures in the interface block must be subroutines that can be referenced as defined assignments.

Each subroutine must have exactly two dummy arguments.

Each argument must be non-optional.

The first argument must have the INTENT(OUT) or INTENT(IN OUT) attribute.

The second argument must have the INTENT(IN) attribute.

A defined assignment is treated as a reference to the subroutine, with the first argument as the assignment target, and the second argument as the expression to be assigned.

The ASSIGNMENT generic specification specifies that the assignment operation is extended or redefined if both sides of the equals sign are of the same derived type.

Example 1

! explicit interfaces interface subroutine ex(a,b,c) implicit none real :: a,b(10,2) integer :: c end subroutine ex function why(t,f) implicit none logical,intent(in) :: t,f logical :: why end function why end interface

Example 2

! generic interfaces module mod1 interface swap module procedure complex_swap ! interface for a module module procedure logical_swap ! procedure is implicit end interface contains subroutine complex_swap(cx,cy) ! implicit interface complex :: cx, cy, ct ! is defined here ct=cx cx=cy cy=ct end subroutine subroutine logical_swap(lx,ly) ! implicit interface logical :: lx,ly,lt ! is defined here lt=lx lx=ly ly=lt end subroutine end module subroutine real_swap(x,y) real :: x.y,t t=x x.y y.t end subroutine subroutine int_swap(ix,iy) integer :: ix,iy,it it=ix ix=iy iy=it end subroutine program interface2 use mod1 interface swap ! extends the interface defined in mod1 subroutine real_swap(x,y) ! explicit interface implicit none real, intent(in out) :: x.y end subroutine real_swap subroutine int_swap(ix,iy) ! explicit interface implicit none integer,intent(in out) :: ix,iy end subroutine int_swap end interface real :: a=1,b=2 integer :: ia=1,ib=2 complex :: ca=(1,1),cb=(2,2) logical :: la=.true.,lb=.false. call swap(a,b) ! calls real_swap write(*,*) a,b call swap(ia,ib) ! calls int_swap write(*,*) ia,ib call swap(ca,cb) ! calls complex_swap write(*,*) ca,cb call swap(la,lb) ! calls logical_swap write(*,*) la,lb end program

Example 3

! operator interfaces module mod1 interface operator (+) ! binary operator module procedure char_plus_int ! implicit interface module procedure int_plus_char ! implicit interface end interface operator (+) interface operator (.not.) ! unary operator module procedure int_not ! implicit interface function real_not(a) ! explicit interface real :: real_not real,intent(in) :: a end function end interface operator (.not.) contains function char_plus_int(c,i) character :: char_plus_int integer,intent(in) :: i character,intent(in) :: c char_plus_int=int_plus_char(i,c) end function char_plus_int function int_plus_char(i,c) character :: int_plus_char integer,intent(in) :: i character,intent(in) :: c integer :: it it=ichar(c)+i if(it < 0) it=0 if(it > 127) it=0 int_plus_char=char(it) end function int_plus_char function int_not(i) integer :: int_not integer,intent(in) :: i int_not=ieor(i,-1) end function end module function real_not(a) real :: real_not real,intent(in) :: a integer :: it it=transfer(a,it) it=ieor(it,-1) real_not=transfer(it,real_not) end function program interface3 ! demonstrate usage use mod1 character :: c="5" integer :: i=-1 real :: r write(*,*) c+i ! calls char_plus_int write(*,*) -i+c ! calls int_plus_char write(*,*) i, (.not. i) ! calls int_not write(*,*) tiny(-r),(.not. huge(r)) ! calls real_not end program

Example 4

! assignment interface module mod4 interface assignment (=) module procedure int_equals_char ! implicit interface end interface assignment (=) contains subroutine int_equals_char(i,c) ! must have two arguments integer,intent(out) :: i ! must be intent(out) ! or intent(in out) character,intent(in) :: c ! must be intent(in) i=ichar(c) end subroutine end module program interface4 use mod4 integer :: i character :: c="a" i=c ! calls int_equals_char write(*,*) i,ichar(c) end program