Search code examples
fortranfunction-pointers

Unable to point to a Fortran function that returns an array


I have a function that returns an array and works fine but I'm unable to point to it. I use the same pointer declaration and assignment that I successfully used with scalar result functions that take the same arguments (reals and a derived type). The declaration is

  PROCEDURE( REAL(idp) ), POINTER, NOPASS :: GradSingleState

Which points to

  this % GradSingleState => GradSingleWell

When I try to compile with gfortran I get the error message

       this % GradSingleState => GradSingleWell
      1 Error: Explicit interface required for ‘’ at (1): array result

and when I compile with Intel and then run it, it crashes.


Solution

  • As we know, when referencing a function which has an array result it is necessary to have an explicit interface available for the function. Your compiler is complaining about there being no explicit interface available here because with procedure pointers there is a similar requirement.

    If GradSingleWell has an array result (requiring an explicit interface to be available when referenced) then the pointer itself must also have an explicit interface. However, in declaring

    PROCEDURE( REAL(idp) ), POINTER, NOPASS :: GradSingleState
    

    the procedure pointer has an implicit interface (and with a scalar result rather than an array result).

    To resolve this, it is required to give the pointer GradSingleState an explicit (and matching) interface:

    PROCEDURE(iface), POINTER, NOPASS :: GradSingleState
    

    where iface is an interface name. Details of how to define iface can be found in a related answer.