Function has no implicit type

前端 未结 4 683
梦如初夏
梦如初夏 2020-12-06 11:07

I am trying to learn to work with functions. I have the following code:

program main
  implicit none

  write(*,*) test(4)
end program

integer function test         


        
相关标签:
4条回答
  • 2020-12-06 11:37

    Just in case, someone has the same problem an alternative way (especially for the case discussed in the comment) is to add

    integer,external :: test
    

    after

    implicit none
    

    in the main program.

    0 讨论(0)
  • 2020-12-06 11:45

    Another simple way, not mentioned in the current answers:

    Move the function before the main program, put module subs, implicit none and contains before the function and end module after the function. The put use subs into your program.

    This way the program can see everything necessary ("explicit interface") about the procedures in the subs module and will know how to call them correctly. The compiler will be able to provide warnings and error messages if you try to call the procedures incorrectly.

    module subs
      implicit none
    contains
      integer function test(n)
        !implicit none no longer necessary here
      end function test
    end module
    
    program main
      use subs
      implicit none
    
    0 讨论(0)
  • 2020-12-06 11:46

    Just put this:

    program main
      implicit none
    

    integer test

      write(*,*) test(4)
    end program
    ...
    

    You need to declare the function as a variable for the compiler to know the return type of the function.

    0 讨论(0)
  • 2020-12-06 11:49

    Move the line

    end program
    

    to the end of your source file and, in its place, write the line

    contains
    

    As you have written your program it has no knowledge of the function test, which is what the compiler is telling you. I have suggested one of the ways in which you can provide the program with the knowledge it needs, but there are others. Since you are a learner I'll leave you to figure out what's going on in detail.

    0 讨论(0)
提交回复
热议问题