Intel® Fortran Compiler Classic and Intel® Fortran Compiler Developer Guide and Reference

ID 767251
Date 3/22/2024
Public
Document Table of Contents

RECURSIVE and NON_RECURSIVE

Keywords: RECURSIVE specifies that a subroutine or function can call itself directly or indirectly. NON_RECURSIVE specifies that a subroutine or function does not call itself directly or indirectly.

Description

RECURSIVE or NON_RECURSIVE can be specified once in a FUNCTION or SUBROUTINE statement. They cannot both appear in the same statement.

Procedures not specified as RECURSIVE or NON_RECURSIVE are currently compiled as NON_RECURSIVE.

The default behavior can be changed by using the -assume [no]recursion option, or in an OPTIONS statement.

NOTE:

The Fortran 2018 Standard specifies that the default mode is recursion; previous standards specified the default was no recursion. The default compilation mode will change to recursion in a future release.

If a function is directly recursive and array valued, and if the keyword RESULT is not specified in the FUNCTION statement, the result variable is the function name, and all occurrences of the function name in the executable part of the function are references to the function result variable.

A directly recursive function cannot have a declared type of CHARACTER if the character length is declared as *.

A procedure interface is always explicit within the subprogram that defines the procedure.

The keyword RECURSIVE must be specified if the compilation mode is set to non-recursive by a compiler option and if any of the following applies (directly or indirectly):

  • The subprogram invokes itself.

  • The subprogram invokes a subprogram defined by an ENTRY statement in the same subprogram.

  • An ENTRY procedure in the same subprogram invokes one of the following:

    • Itself

    • Another ENTRY procedure in the same subprogram

    • The subprogram defined by the FUNCTION or SUBROUTINE statement

The keyword NON_RECURSIVE must be specified if the compilation mode is set to recursion by a compiler option and the procedure is not to be compiled for recursion.

Example

 ! RECURS.F90
 !
   i = 0
   CALL Inc (i)
   END
   RECURSIVE SUBROUTINE Inc (i)
   i = i + 1
   CALL Out (i)
   IF (i.LT.20) CALL Inc (i)    ! This also works in OUT
   END SUBROUTINE Inc

   SUBROUTINE Out (i)
   WRITE (*,*) i
   END SUBROUTINE Out