我收到此错误消息,功能“ areacircle”需要一个参数列表,为什么?

问题描述

这是程序。我为什么会出错?

  ''''code'''''

我不知道为什么整体不出现,我试图确定随机数的面积和体积。 - - - - - - - - 为什么 - - - - - - - '''Fortran

'program exercise2'
!
      integer :: N,i
      type :: Values
      double precision :: radius,area,volume
      end type
!
!
      type(Values),allocatable,dimension(:) :: s
      integer :: bi
!
!Read the data to create the random number
      write(6,*) 'write your number '
      read(5,*) N
      allocate(s(N))
      bi = 3.14
!create the random number
      call random_seed()
      do i=1,N
        call random_number(s(i)%radius)
        s(i)%area=areacircle(s(i)%radius)
        s(i)%volume=volumesphere(s(i)%radius)
      end do
!
      open(15,file='radius.out',status='new')
      write(15,*) s(i)%radius
      open(16,file='output2.out',status='new')
      r = real(s(i)%radius)
!Two function
      contains
      
      double precision function areacircle (s)
      implicit none
      double precision :: s
      do i=1,N
         areacircle=bi*r**2
      end do
      return
      end function areacircle
!
!
      double precision function volumesphere (s)
      implicit none
      double precision :: s
      do i=1,N
         volumesphere=4/3*bi*r**3
      end do
      return
      write(16,*)  r,areacircle,volumesphere
      end function volumesphere
'end program exercise2'

''''''' 所以有人知道为什么吗?

解决方法

这可能会满足您的要求。由于面积和体积的计算只涉及一个不变的输入,因此我将您的函数更改为elemental。这允许使用数组参数,其中对数组的每个元素执行函数。我还更改了double precision以使用Fortran种类类型参数机制,因为键入real(dp)要短得多。 最后,切勿在没有implicit none语句的情况下编写Fortran程序。

program exercise2

  implicit none ! Never write a program without this statement

  integer,parameter :: dp = kind(1.d0)  !  kind type parameter
  integer n,i
  type values
     real(dp) radius,area,volume
  end type

  type(values),allocatable :: s(:)
  real(dp) bi  ! integer :: bi?

  ! Read the data to create the random number
  write(6,*) 'write your number '
  read(5,*) n

  ! Validate n is validate.
  if (n < 1) stop 'Invalid number'

  allocate(s(n))

  bi = 4 * atan(1.d0)  !   bi = 3.14?  correctly determine pi

  call random_seed()                  ! Use default seeding
  call random_number(s%radius)        ! Fill radii with random numbers
  s%area   = areacircle(s%radius)     ! Compute area  
  s%volume = volumesphere(s%radius)   ! Compute volume

  write(*,'(A)') '   Radii    Area    Volume'
  do i = 1,n
     write(*,'(3F9.5)') s(i)
  end do

  contains
  
     elemental function areacircle(s) result(area)
        real(dp) area
        real(dp),intent(in) :: s
        area = bi * s**2
     end function areacircle

     elemental function volumesphere(s) result(volume)
        real(dp) volume
        real(dp),intent(in) :: s
        volume = (4 * bi / 3) * s**3
     end function volumesphere
end program exercise2