variables - Fortran 函数/子程序中的任意变量/数组类型

标签 variables fortran subroutine

我希望这不是一个太愚蠢的问题,但是是否可以有函数或子例程来传递数组的类型,例如

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/

相关文章:

arrays - 在Fortran中访问字符串中特定索引处的字符

perl - 为什么语法 `&name arg1 arg2 ...` 不能用于调用 Perl 子程序?

在转换中引用新列

JavaScript:将字符串转换为不同的格式

fortran - MPI_Gather 在最基本的代码中给出了 seg 错误

Fortran 使用 gdb 从地址打印函数名

module - 如果可选参数不存在,我们可以避免创建局部变量吗?

perl - 是否可以只对 Perl 中的子例程调用进行计时?

objective-c - Objective-C 中的变量声明差异

C++ 哪个函数更快?重复赋值或重复创建