Fortran/language extensions

Procedure Overloading
Like several other languages, Fortran 90 and newer supports the ability to select the appropriate routine from a list of routines based on the arguments passed. This selection is done at compile time and is thus unencumbered by run-time performance penalties. This feature is accessed by use of modules and the interface block.

In the following example, a module is specified which contains an interface function  which can handle arguments of various types.

A program which uses this module now has access to a single interface function  which accepts arguments that are of integer, real, or complex type. The return type of the function is the same as the input type. In this way the routine is much like many of the intrinsic functions defined as part of the Fortran standard. An example program is given below:

Intrinsic functions
One can extend intrinsic functions. This is similar to overload operators.

Here we will demonstrate this by extending the  function. The intrinsic function is not implemented for arguments of integer type. This is because there is no clear idea how to define the result of non integer type (e.g. $$\sqrt{4}=2$$, but how to define $$\sqrt{5}=2.236 \ldots$$). We implement a method here where the result is always the nearest integer.

Derived Data Types
Fortran 90 and newer supports the creation of new data types which are composites of existing types. In some ways this is similar to an array, but the components need not be all of the same type and they are referenced by name, not index. Such data types must be declared before variables of that type, and the declaration must be in scope to be used. An example of a simple 2d vector type is given below.

Variables of this type can be declared much like any other variable, including variable characteristics such are pointer or dimension.

Using derived data types, the Fortran language can be extended to represent more diverse types of data than those represented by the primitive types.

Operator Overloading
Operators can be overloaded so that derived data types support the standard operations, opening the possibility of extending the Fortran language to have new types which behave nearly like the native types.

Assignment
The assignment operator = can be overloaded. We will demonstrate this by the following example. Here, we define how the assignment of a logical type on the left and an integer on the right should be performed.

Intrinsic operators
One can overload intrinsic operators, such as.

In the following example we will overload the  operator to work as the logical.

New operators
One can create newly self-created operators.

We demonstrate this by the following example: We create an unary operator  which outputs a   if the given   is even as well as a binary operator   that performs the standard cross product of two   vectors.