包含fortran

时间:2017-05-28 11:02:48

标签: module fortran procedure

我正在学习正确使用子程序,函数和模块,下面是一个简单的例子,编译时没有错误,执行后,结果是4.57187637E-41而不是pi,我已经抬头了几个参考文献,还没有找到错误。

module wz
   implicit none
   private
   public :: print_out
   real,parameter :: pi = 3.14159
   contains
      subroutine print_out
      implicit none
      real :: area
      print *, area
      end subroutine print_out

      function f(x) result(area)
      implicit none

      real, intent(in):: x
      real            :: area

      area = pi * x ** 2
      end function f
end module wz

program test_module
use wz
implicit none
    real :: x
    x = 1.
   call print_out
end program test_module

1 个答案:

答案 0 :(得分:2)

您正在打印的值是area,只是在声明之后和之前做任何事情。您需要将x传递给f函数,并且可以通过print_out子例程执行此操作:

module wz
   implicit none
   private
   public :: print_out
   real,parameter :: pi = 3.14159
   contains
      subroutine print_out(x)
      implicit none
      real, intent(in) :: x
      real :: area
      area = f(x)
      print *, area
      end subroutine print_out

      function f(x) result(area)
      implicit none

      real, intent(in):: x
      real            :: area

      area = pi * x ** 2
      end function f
end module wz

program test_module
use wz
implicit none
    real :: x
    x = 1.
   call print_out(x)
end program test_module