FUNCTION - Fortran

The FUNCTION statement is the first statement of a function subprogram.
It specifies the symbolic name of the function and its type.


[typ] FUNCTION fun [*len] ([d[,d]…])


  • typ optionally specifies the data type of the function name, which determines the value returned to the calling program. The following forms for typ are allowed:
  • fun is a symbolic name of the function subprogram in which the FUNCTION statement appears.
  • len specifies the length of the data type; fun must be a nonzero, unsigned constant. Do not specify len when the function is type CHARACTER with an explicit length following the keyword CHARACTER.

w is a dummy argument and can be a variable, array name, or dummy subprogram name.

Rules for Use

  • A FUNCTION statement must appear only as the first statement of a function subprogram.
  • The type specification may be omitted from the FUNCTION statement, and the function name may be specified in a type statement in the same program unit. If neither of these options is used, the function is implicitly typed.
  • The symbolic name of a function is a global name and must not be the same as any other global or local name, except a variable name, in the function subprogram.
  • If the function type is specified in the FUNCTION statement, the function name must not appear in a type statement.
  • In the type specification CHARACTER, len can have any of the forms allowed in a CHARACTER statement, except that an integer constant expression must not include the symbolic name of a constant. If the name of the function is type character, then each entry name in the function subprogram must be type character. If the length is declared as an asterisk, all such entries must have a length declared with an asterisk.
  • A function specified as a subprogram may be referenced within any other subprogram or in the main program of the executable program.


  • A function subprogram cannot contain a BLOCK DATA, SUBROUTINE, or PROGRAM statement.
  • A function name cannot have its type explicitly specified more than once in a program unit.
  • In a function subprogram, a dummy argument name cannot appear in an EQUIVALENCE, PARAMETER, SAVE, INTRINSIC, DATA, or COMMON statement, except as a common block name.
  • A character dummy argument with a length specified as an asterisk must not appear as an operand for concatenation, except in a character assignment statement.
  • The compiler system permits recursion if the automatic storage of variables is in effect. The –automatic command line option , by default, causes the automatic storage of variables.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

Fortran Topics