我无法解决脚本中的错误

I cannot work out the error in the script

我是编程新手,无法调试我的程序。每当我 运行 它给出同样的错误:

return type mismatch of function f at (1)

我的代码是:

      real function F(x)
        implicit none
        real:: x
        F=exp(-x)-x
      end function

      program easycod
        implicit none
        real::xl,xu,xr,fu,test,xrold,fl,fr
        integer ::i
        do i=1,50
          xr=xrold
          xr=(xl+xu)/2.0
          fr =F(xr)
          fl =F(xl)
          test=fl*fr 
          IF (test>0.0) then
            xl=xr 
            fr=fl
          else if (test<0.0) then
            xu=xr 
          end if
          if (test==0.0) exit 
          print*,xr
        end do
      end program

您的代码有一些问题。

首先,您收到编译器错误,因为您尚未在 program:

中声明函数 F
program easycode
    implicit none
    real :: xl, xu, xr, fu, test, xrold, fl, fr
    real :: F                    ! <----------------- Add this line
    integer :: i

然后你分配 xr 两次,这使得第一个不必要。最后,xoldxlxr 没有初始化,因此可以赋予编译器想要的任何值。