Fortran Coder

查看: 8140|回复: 2

[子程序] 子程序的某个形参是函数

[复制链接]

17

帖子

6

主题

0

精华

入门

F 币
107 元
贡献
54 点
发表于 2018-6-13 23:24:03 | 显示全部楼层 |阅读模式
有一段程序:
[Fortran] 纯文本查看 复制代码
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如下。
[Fortran] 纯文本查看 复制代码
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)),不能我自己定义
我用
[Fortran] 纯文本查看 复制代码
external::f
dimension(2)::f
在主程序定义以后,程序报错如图
捕获.PNG

130

帖子

10

主题

0

精华

大师

F 币
617 元
贡献
372 点

贡献勋章管理勋章帅哥勋章元老勋章星光勋章规矩勋章

发表于 2018-6-14 00:57:54 | 显示全部楼层
[Fortran] 纯文本查看 复制代码
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

17

帖子

6

主题

0

精华

入门

F 币
107 元
贡献
54 点
 楼主| 发表于 2018-6-14 08:10:43 | 显示全部楼层
谢谢!没系统学过fortran,不知到interface还能这么用……真的谢谢啦
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-3-29 14:29

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表