9 Generic Procedures and Overloading
9.1 Generic Procedures A generic procedure allows the call of a procedure by a single name while the arguments may have different data types (and the result in case of a function call as well). A procedure must be defined for each (valid) set of argument data types. The procedure applied when calling a generic procedure depends on the data types of the arguments. Several internal procedures e.g. sin(x), sqrt(x) are generic procedures and can be called with arguments of different type, e.g. real, double precision or even with array arguments. The data type of the return value corresponds to the arguments data type. A generic procedure is defined by use of an INTERFACE block. It specifies the generic name as well as the set of procedures that will be executed when the procedure is called. All the procedures specified must have a unique set of argument-types. The data type of the arguments determines what procedure is executed.
INTERFACE generic_name
- interface block 1
- ..
- END INTERFACE
generic_name name of the generic procedure interface block Interface definition for the according procedures
Please see Example 1 Please do Task 9.1
9.2 Overloading FORTRAN95 allows overloading of operator including the assignment operator (=). Overloading can only extend the already existing operators for new (and unique sets of) data types (of the operands). Modules often are used for providing access to assignment and operator overloading especially in connection with a derived data type definition. A module can be used to introduce new (derived) data types and operator applicable to operands of that data type. The same is true for overloading of the assignment operators in order to work with that data type. The same is true for overloading of the assignment operator in order to work with that data type. 9.2.1 Overloading of intrinsic Operators Intrinsic operators can be extended for usage of additional data types. This requires an INTERFACE block in the form:
- INTERFACE OPERATOR (internal operator)
- interface Block
- END INTERFACE
while the INTERFACE block specifies the functions which defines the operation. The function provided for an operator overload must fit some condition depending on the operator. Please see Example 2
9.2.2 Assignment Overloading The assignment (=) overloading is similar to an overloading for intrinsic operators. It requires an INTERFACE block in the following form:
- INTERFACE ASSIGNMENT (=)
- interface Block
- END INTERFACE
It requires the definition of the assignment by a SUBROUTINE with two arguments. The first argument (right hand side) must have the attribute INTENT(IN) while the second argument (left hand side) must have the attribute INTENT(OUT). Please see Example 3 Please do Task 9.2
9.3 Defining new Operators It is possible to define new operators. They must have the form .OPERATOR. An operator is defined in the same way as operators are overloaded. An INTERFACE block is required which has the form:
- INTERFACE OPERATOR (.OPERATOR.)
- interface Block
- END INTERFACE
The functions associated with the .operator. must have one or two arguments with attribute INTENT(IN). Please see Example 4 Please see Tasks 9.3 und 9.4