0

I get an error in line 7 that fun is not a function. I have no idea how to fix this, to me it seems that it is a functions. I saw a lot of examples of using external functions as arguments for procedures(functions and subroutines). I am trying to do this in Fortran 77 or Fortran 90.

      program integral
      implicit none
      external fun
      double precision a,b,results,help
      integer n
      results = 0
      call integrate(fun,a,b,n,results)
      read(5,*) a,b,n
      write(6,*) results
      end program integral

      function fun(x)
      implicit none
      double precision fun
      double precision x
      fun = 4*x*x
      return
      end function fun

      subroutine integrate (f,a,b,n,results)
      implicit none
      integer i,n
      double precision a,b,h,results,f
      results= 0
      h = (b-a)/n
      do i=0,n-1
      results = results + f(a+i*h)
      end do
      return
      end subroutine integrate
  • Welcome, please take the [tour] and read [ask]. You should copy and paste the actual error message into your question. Saying *"I get an error in line 7 that fun is not a function."* is not really enough. Do not say what the error said, just show us the error message. – Vladimir F Героям слава Oct 20 '22 at 09:05
  • You must either include your procedures into a module - that is the right way in Fortran 90 and later, or declare the type of `fun` in your program. It is not visible when it is external. We have many duplicates, although most often for Fortran 90 plus. There should not be a good reason for sticking to Fortran 77 in this century and your code is NOT Fortran 77 conforming anyway. – Vladimir F Героям слава Oct 20 '22 at 09:07
  • Remember, the only thing that the program sees about `fun` is `external fun` so that the fact that it seems that it is a function to you when looking at the code of the function is irrelevant. It is not visible from the main program this way. You must at least declare the type as `double precision`. – Vladimir F Героям слава Oct 20 '22 at 09:16
  • Also, depending on the compiler, I get nothing or I get just a warning or I get an error. You should always include the actual error output, the command that caused it and the compiler version. The code actually works if it gets compiled, because internally i=the interface mismatch does not matter in the end. – Vladimir F Героям слава Oct 20 '22 at 09:21
  • Lets use it as a duplicate. Although the code in question does not have the "no implicit type" error message, it will have it as soon as one tries to call the function, e.g., by printing `fun(1.d0)`. – Vladimir F Героям слава Oct 20 '22 at 11:16

0 Answers0