Pregunta

I am running a .f90 resource file in Visual Studio 2008, using Intel Visual Fortran 11.1.048. Below you can see a test code.

I get this odd error after running it: "the type of the function reference does not match the type of the function definition. [G]" And I have tried to figure it out, but nothing. Have I done something wrong or there is a problem with the software. Can anyone help?

Program TEST

real*8,Dimension(10) :: z, f_x
real*8 a

a = 2.0
z(1) = 0.0

    do i = 2,10
        z(i) = 1 + z(i-1)
    end do

    do i=1,10
        f_z(i)= a*g(z(i))
    end do

End program TEST


    FUNCTION g(z)


    Real*8 z, g0, g
    g0=1

    g=g0*cos(z)
    return
    END FUNCTION g
¿Fue útil?

Solución

That's a horrid piece of code ...

The cause of your problem is that the name g in the program is related only by name to the function called g. You haven't told the compiler that they are the same things, and it is left to the runtime to connect them. Because you have used implicit typing something in the program scope called g is given type real. But your function called g returns a real*8 value, leading directly to the error message you see.

To fix this:

  1. Use explicit typing in all scopes. Add the line implicit none at the start of each program unit. Then clear up the errors that arise.
  2. Get the compiler to generate an explicit interface to the function g. The easy way to do this is to move the end program statement to the end of your source file and in its place write the word contains. This will enable the compiler to know that the name g refers to that function and not to some variable you haven't declared.
  3. Drop the use of the non-standard real*8 (etc). One option for this would be to use-associate the standard module iso_fortran_env with a line such as

    use, intrinsic :: iso_fortran_env

    between the program statement and implicit none and then you can use pre-defined kinds for your declarations like this;

    real(real64) :: g0

  4. Indent your program consistently.

There's probably more, but that's enough for now.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top