FUNCTION statement

Standard: (F77) F90 F95 F2003 F2008 F2018 Example program


[ prefix ]... FUNCTION name args [ suffix ]...

args ::= ( [ name [ , name ]... ] )

prefix ::= ELEMENTAL
| type-spec

suffix ::= BIND ( C [ , NAME=expression ] )
| RESULT(name)

Only one occurrence of any particular prefix or suffix is allowed in a single FUNCTION statement. If IMPURE appears, PURE must not appear. If MODULE appears, the interface or definition must be for a separate module procedure. If NON_RECURSIVE appears, RECURSIVE must not appear.

The FUNCTION statement can only occur as the first statement of a function subprogram or interface body. The syntax of a function subprogram is as follows.
      [ contains-stmt
            [ internal-subprogram ]... ]
An internal-subprogram is a function-subprogram or a subroutine-subprogram, and is not allowed to itself have a contains-stmt.

The syntax of a function interface body is simply:



The FUNCTION statement begins a function subprogram or interface body. If no RESULT variable is specified the result variable has the same name as the function name. If the type is not specified on the FUNCTION statement, it may be implicitly typed or the result variable may be explicitly declared.

Note that for direct recursion, a RESULT clause is necessary as well as the RECURSIVE attribute. In Fortran 2018, a procedure without NON_RECURSIVE has the RECURSIVE attribute by default, except for a CHARACTER(LEN=*) function.


BIND attribute, CALL statement, CONTAINS statement, ELEMENTAL attribute, END FUNCTION statement, ENTRY statement, IMPURE attribute, NON_RECURSIVE attribute, PURE attribute, RECURSIVE attribute, RESULT clause, RETURN statement, Separate module procedures SUBROUTINE statement