lxxx 发表于 2018-6-13 23:24:03

子程序的某个形参是函数

有一段程序:
program ode
use rksuite_90
implicit none
……
external::f
dimension(2)::f
……
call range_integrate_r1(comm,f,t_want,t_got,y_got,yderiv_got,flag)
stop
end program ode

function f(t,y)
use rksuite_90
implicit none
real(kind=wp)::t
real(kind=wp)::y(2)
real(kind=wp)::f(2)   !声名返回值类型
f(1)=y(2)
f(2)=-y(1)
end function
module如下。
module rksuite_90
……
recursive subroutine range_integrate_r1(comm,f,t_want,t_got,y_got,yderiv_got,flag)
interface
      function f(t,y)
      use rksuite_90_prec, only:wp
      real(kind=wp), intent(in) :: t                                 !indep!
      real(kind=wp), dimension(:), intent(in) :: y                   !dep!
      real(kind=wp), dimension(size(y,1)) :: f                     !dep!
      
   end function f

end interface
……
comm%yp = f(comm%t,comm%y)
……
end module rksuite_90
range_integrate_r1中的形参f是个function,由于 f 在module中的定义是dimension形式的,见module代码,(size=2),
所以需要将 f 以external且以一个数组的形式传给range_integrate_r1,请问应怎么做?
f(t,y)中的形参数值是在其他子程序中给定的,见module代码(语句为comm%yp = f(comm%t,comm%y)),不能我自己定义
我用
external::f
dimension(2)::f在主程序定义以后,程序报错如图

chiangtp 发表于 2018-6-14 00:57:54

module rksuite_90_prec
implicit none
integer, parameter :: wp=selected_real_kind(P=15)
end module rksuite_90_prec

module rksuite_90
implicit none
contains
recursive subroutine range_integrate_r1(comm,f,t_want,t_got,y_got,yderiv_got,flag)
    implicit none
    real :: comm, t_want, t_got, y_got, yderiv_got, flag
    interface
       function f(t,y)
          use rksuite_90_prec, only:wp
          implicit none
          real(kind=wp), intent(in) :: t
          real(kind=wp), dimension(:), intent(in) :: y
          real(kind=wp), dimension(size(y,1)) :: f
       end function f
    end interface
    !...
end subroutine range_integrate_r1
end module rksuite_90

program ode
use rksuite_90
implicit none
real :: comm, t_want, t_got, y_got, yderiv_got, flag
interface
   function f(t,y)
       use rksuite_90_prec, only:wp
       implicit none
       real(kind=wp), intent(in) :: t
       real(kind=wp), dimension(:), intent(in) :: y
       real(kind=wp), dimension(size(y,1)) :: f
   end function f
end interface

call range_integrate_r1(comm,f,t_want,t_got,y_got,yderiv_got,flag)

end program ode

function f(t,y)
use rksuite_90_prec, only:wp
implicit none
real(kind=wp), intent(in) :: t
real(kind=wp), dimension(:), intent(in) :: y
real(kind=wp), dimension(size(y,1)) :: f
f(1) = y(2)
f(2) =-y(1)
end function f

lxxx 发表于 2018-6-14 08:10:43

谢谢!没系统学过fortran,不知到interface还能这么用……真的谢谢啦
页: [1]
查看完整版本: 子程序的某个形参是函数