Fortran function in a module

conflicts with symbol from module

Posted by JXLIU on August 21, 2022

Fortran function in a module

function out of a module

the function need to be declared when it is used in a subroutine or main program.

      program test
      implicit none
      real:: f
      external:: f
      real:: x
      x=1.0
      call tests(x)
      print*,f(x)
      end program test

      subroutine tests(x)
      implicit none
      real x,f
      print*,f(x)
      end

      function f(x)
      implicit none
      real:: f
      real::x
      f=x
      end function f

If the function is not declared, there will be such an error

1
undefined reference to `__func_MOD_f

function in a module

the function can not be declared when it is used in a subroutine or main program.

      module func
      contains
      function f(x)
      implicit none
      real:: f
      real::x
      f=x
      end function f
      end

      program test
      use func
      implicit none
!      real:: f
!      external:: f
      real:: x
      x=1.0
      call tests(x)
      print*,f(x)
      end program test

      subroutine tests(x)
      use func
      implicit none
      real x     !,f
      print*,f(x)
      end

If f is declared in the subroutine or main program, there will the such an error

1
2
3
4
5
6
   12 |       use func
      |         2     
   13 |       implicit none
   14 |       real:: f
      |              1
Error: Symbol ‘f’ at (1) conflicts with symbol from module ‘func’, use-associated at (2)