Function result has no implicit type

后端 未结 2 859
面向向阳花
面向向阳花 2021-01-22 11:04

Below is a sample code that addresses the problem I am having. The error message I am getting is

Function result \'sample\' at (1) has no IMPLICIT type.<

2条回答
  •  北海茫月
    2021-01-22 11:07

    Procedures in Fortran come in two types: functions and subroutines. This question is about functions, so I'll consider just those.

    What was missing in the first revision, giving the error about the implicit type of the function result1, was the result type.

    Adding real function ... or complex function ..., etc., resolves that problem by explicitly giving the type of the function result. The linked documentation gives other ways of doing that.

    The function's result is used when the function is referenced. When we have a reference like

    func0 = Sample(func)
    

    in the main program, the function Sample is invoked and the function result is defined in its execution. At the end of the function's execution its result is placed in the expression of the reference.

    So, if you declare

    real function Sample(func)
    

    or

    complex function Sample(func)
    

    what you are saying is that the function result is either a real or complex entity. And when the function is evaluated, whatever value Sample had at the end is used in the expression (here assignment).

    As a consequence of the function result being returned through Sample (in this case) we need to define its value. The important thing to note for the question, then, is that LocalF is a variable local to the function. If you mean it to be the result of the function you need to use the function result.

    You have a number of options:

    function Sample(func)
      ,  :: sample  ! Instead of LocalF
      ...                  :: func
    end function
    

    or

    function Sample(func) result(LocalF)
      ,  :: LocalF
      ...                  :: func
    end function
    

    You can even have

     function Sample(func)
      
      ... func
    end function
    

    but I really suggest you avoid that last one.


    1 Note the error here is about type for the function result; in the linked question simply about the function when referenced.

提交回复
热议问题