Search code examples
functionfortrangfortran

Functions in fortran


I am totally new in programming in fortran..... I tried to make a function, which is called in another program....

So I have my main Programm, which is called Spectrum.f:

      program Spectrum

       external I_Analytic
       double precision I_Analytic

       write(*,*) I_Analytic(5.D0)

      end

The function I_Analytic is defined in a second document, called Analytic.f:

       DOUBLE PRECISION function I_Analytic(w1)

        I_Analytic = w1**2

       end function I_Analytic

After compiling this program with gfortran -o Spectrum Analytic.f Spectrum.f

I only get 0.0000000000 instead of 25....What is wrong with my program?


Solution

  • Your function does not declare the type of w1 so by the implicit typing rules it is real, not double precision. As I suggested in the comment, always use IMPLICIT NONE, and preferably also use modules to get an error if the interface does not conform.