| United States-English |
|
|
|
![]() |
HP Fortran 90 Programmer's Reference: HP Fortran 90 Programmer's Reference > Chapter 7 Program units and proceduresProcedure interface |
|
A procedure interface is the information specified in a procedure reference, including the name of the procedure, the arguments, and (if the procedure is a function) the result. If the interface is explicit, all of the characteristics of the arguments and the result—type, kind, attributes, and number—are defined within the scope of the reference. If the interface is implicit, the compiler may be able to make sufficient assumptions about the interface to permit the procedure reference. All procedure interfaces are implicit except for the following:
An explicit interface is required when:
The following sections describe the interface block and its use for creating:
An interface block is used to provide an explicit interface for external procedures or to define a generic procedure. An interface block may appear in any program unit, except a block data program unit. It is specified in the specification part of the program unit. The syntax for an interface block is:
The following example, proc_interface.f90, uses an interface block in the main program unit to provide an explicit interface for the function avg. Example 7-8 proc_interface.f90
Here are the command lines to compile and execute the program, along with the output from a sample run:
The Fortran 90 concept of generic procedures extends the FORTRAN 77 concept of generic intrinsics to allow user-defined generic procedures. A procedure is generic if its name—a generic name—is associated with a set of specific procedures. Referencing the generic name allows actual arguments to differ in type, kind, and rank. The differences in the arguments determine which specific procedure is invoked. A generic procedure is defined in an interface block that specifies its name and the interfaces of the specific procedures; see “Interface blocks”. The specific procedures within the interface block must all be subroutines or all functions. The interface for each procedure must differ from the others in one or more of the following ways:
There may be more than one interface block with the same generic name, but the specific procedures whose interfaces appear in all such interface blocks must be distinguishable by the above criteria. The MODULE PROCEDURE statement can be used to extend the list of specific procedures to include procedures that are otherwise accessible to the program unit containing the interface block. The MODULE PROCEDURE statement specifies only the procedure names; the procedure interfaces are already explicit. The MODULE PROCEDURE statement may appear only in an interface block that has a generic specification. Furthermore, the interface block must be contained either in the same module that contains the definitions of the named procedures or in a program unit in which the procedures are accessible through use association. The following example assumes that two subroutines have been coded for solving linear equations: rlineq for when the coefficients are real, and zlineq for when the coefficients are complex. A generic name, lineq, is declared in the INTERFACE statement, enabling it to be used for referencing either of the specific procedures, depending on whether the arguments are real or complex:
The OPERATOR clause can be used with the INTERFACE statement either to define a new user-defined operator or to extend—or overload—the behavior of an already defined or intrinsic operator. This second use is similar to defining a generic procedure (see “Generic procedures”). The re-defined operator becomes associated with a generic operator. When the OPERATOR clause is present in the INTERFACE statement, the specific procedures within the interface block must all be functions. The functions can implement the operator for operands of different types, kinds, and ranks. These functions are restricted to one or two mandatory arguments, depending on whether the defined operator is unary or binary. The functions return the result of an expression of the form:
Each dummy argument of the functions listed in the interface block must have the INTENT(IN) attribute. If operator is intrinsic, each specified function must take the same number of arguments as the intrinsic operator has operands. Furthermore, the arguments must be distinguishable from those normally associated with the intrinsic operation. However, argument keywords must not be used when the argument is specified as an operand to a defined operator. If a user-defined operator is referenced by its generic name, the reference must resolve to a unique, specific function name. The selection of the function is accomplished by matching the number, type, kind, and rank of the operand with the dummy argument lists of the functions specified in the interface block. As with generic name references (see “Generic procedures”), exactly one procedure must match the properties of the operands, and the matching function is selected and invoked. The following program, def_op.f90, illustrates a defined operation. The operation, .inrect., compares two derived-type operands. The one operand holds the x and y co-ordinates of a point on a graph, and the other holds the set of co-ordinates defining a rectangle. If the point is inside the rectangle, the operation evaluates to .TRUE.. The module in which the operation is defined also contains the definitions of the types of the operands. As noted in the comments, when a module is defined in the same file as any USE statements that reference the module, the definition must lexically precede the USE statements. For information about modules and the USE statement, see “Modules”. Example 7-9 def_op.f90
Here are the command lines to compile and execute the program, along with the output from a sample run:
The ASSIGNMENT clause can be used with the INTERFACE statement to specify one or more subroutines that extend—or overload—the assignment operator. Each subroutine must have exactly two arguments. The first argument can have either the INTENT(OUT) or the INTENT(INOUT) attribute; the second argument must have the INTENT(IN) attribute. The first argument corresponds to the variable on the left-hand side of an assignment statement, and the second to the expression on the right-hand side. Similarly to generic names and defined operators, there can be more than one defined assignment, but each occurrence of the assignment statement must resolve to a unique, specific subroutine. The subroutine whose dummy arguments match the left-hand and right-hand sides of the assignment statement in kind, type, and rank is selected and invoked from the list of subroutines specified in the defined-assignment interface block. The following example, def_assign.f90, illustrates defined assignment. The assignment consists of performing an elementary statistical analysis of the data on the right-hand operand and storing the results in the left-hand operand. As noted in the comments, when a module is defined in the same file as any USE statements that references the module, the definition must lexically precede the USE statements. For information about modules and the USE statement, see “Modules”. Example 7-10 def_assign.f90
Here are the command lines to compile and execute the program, along with the output from a sample run:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||