我希望这不是一个太愚蠢的问题,但是是否可以有函数或子例程来传递数组的类型,例如
subroutine foo(array, arr_type)
implicit none
arr_type, dimension(:) :: array
*Do something with array*
end subroutine
或者我是否必须为每个可能的 arr_type (例如整数、 double ……)编写一个子例程并使用接口(interface)重载该子例程?
最佳答案
是和否...您可以使用接口(interface)将多个函数/子例程与不同的虚拟参数捆绑在一起:
module foos
interface foo
module procedure foo_real
module procedure foo_int
end interface
contains
subroutine foo_real( a )
implicit none
real,intent(inout) :: a(:)
! ...
end subroutine
subroutine foo_int( a )
implicit none
integer,intent(inout) :: a(:)
! ...
end subroutine
end module
我知道没有(简单)可能传递任意基本类型的数组。你可以看看transfer
- 但有龙;-)
关于variables - Fortran 函数/子程序中的任意变量/数组类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21109558/