Fortran subprograms are defined in code blocks that begin with a function or subroutine statement. As is customary in programming languages, the function or subroutine keyword is followed by a list, in parentheses, of dummy arguments (parameters) identifying the arguments that are expected in a call to the subprogram.*

In Fortran, all arguments are passed by reference, i.e., they refer back to memory locations in the caller:

  • This means that the arguments that are passed to a subprogram give the subprogram direct access to the corresponding data residing in the caller.
  • Therefore, the datatypes of all the expected arguments must be declared in the subprogram.

One additional declaration is associated with a function, because it must return one or more values: a scalar, a string, an array, etc. Accordingly, the function's name must be given a type. This can be done either by:

  • inserting the type of the return value(s) at the beginning of the function statement, or
  • declaring the function name as a variable in the declaration section.

The declaration section may also specify local variables that are accessible only from within the subprogram. Subroutines can only return values through the argument list, so the name of a subroutine does not require a type.

A Fortran subprogram always ends with the return and end statements, so the return value of a function has to be assigned before return is called. Here is an example of a function (which happens to be equivalent to the Fortran intrinsic function dot_product for real-valued vectors):

The values of parameters (i.e., dummy arguments) may be changed in subprograms. It is good Fortran programming style to write functions that do not change the parameters, but only return a value (or array of values, etc.). Functions are then free of side-effects, which increases the maintainability of the code and also helps the compiler with optimization. Again, subroutines can only output data by changing the parameters.

Incidentally, the prior example and the next feature adjustable arrays among the parameters, i.e., arrays whose dimensions are determined from other parameters. This syntax is a holdover from F77. In F90 and successors, it is often preferable to declare such arrays as assumed-shape arrays, as described in a subsequent section.

Fortran 90 also made it possible to declare explicitly the intent to use a parameter for input, output, or both. Here is an example of a subroutine that makes use of the intent attribute:

*Terminology note: in computer programming, parameters appear in procedure definitions, while arguments appear in procedure calls. Parameters thus act as placeholders for the actual arguments, which is why they are sometimes referred to as dummy arguments. In Fortran, the distinction between parameters and arguments becomes even more confusing because the parameter attribute is used to declare constants. Therefore, the term "dummy argument" is usually preferred over "parameter", when discussing Fortran subprograms.

 
©  |   Cornell University    |   Center for Advanced Computing    |   Copyright Statement    |   Inclusivity Statement