Developer Guide and Reference

ID 767251
Date 10/31/2024
Public
Document Table of Contents

RESULT

Keyword: Specifies a name for a function result.

Description

Normally, a function result is returned in the function's name, and all references to the function name are references to the function result.

However, if you use the RESULT keyword in a FUNCTION statement, you can specify a local variable name for the function result. In this case, all references to the function name are recursive calls, and the function name must not appear in specification statements.

The RESULT name must be different from the name of the function.

Example

The following shows an example of a recursive function specifying a RESULT variable:

  RECURSIVE FUNCTION FACTORIAL(P) RESULT(L)
    INTEGER, INTENT(IN) :: P
    INTEGER L
    IF (P == 1) THEN
      L = 1
    ELSE
      L = P * FACTORIAL(P - 1)
    END IF
  END FUNCTION

The following shows another example:

  recursive function FindSame(Aindex,Last,Used) &
 &  result(FindSameResult)
  type(card) Last
  integer Aindex, i
  logical matched, used(5), FindSameResult
  if( Aindex > 5 ) then
    FindSameResult = .true.
    return
  endif
  ...