具有假定形状虚拟参数的过程必须具有显式接口 [英] Procedure with assumed-shape dummy argument must have an explicit interface
问题描述
我对 Fortran 90 完全陌生,我正在尝试了解如何将数组传递给函数.我在网上查了一下,我找不到任何足够清晰和简单的例子,所以我决定在这里发布.
我希望函数能够处理任意长度的数组(数组的长度不应该是函数的参数之一).
我试着写一个简单的例子,一个返回数组元素总和的函数:
函数 mysum(arr)隐式无真实,维度(:),意图(输入):: arr真的 :: mysum整数 :: i,arrsizearrsize = 大小(arr)我的总和=0.0do i=1,arrsizemysum=mysum+arr(i)端部结束函数 mysum程序测试隐式无实数,维数(4) :: a真实 :: mysum,a_sum调用 random_number(a)打印 *,aa_sum=mysum(a)打印 *,a_sum结束程序
当我尝试编译时,出现以下错误:
array_test.f90:17.14:真实的mysum,a_sum1错误:(1) 处的过程 'mysum' 具有假定形状的虚拟参数 'arr' 必须具有显式接口
我的程序有什么问题?
Assumed shape dummy arguments(那些带有 (:)
的)需要显式接口到过程可在呼叫站点获得.这意味着调用代码必须知道子例程头的确切样子.另请参阅模块调用具有隐式接口的外部过程>
可以通过多种方式提供该显式接口
1.首选 - 一个模块程序
模块程序隐式无包含函数 mysum(arr)真实,维度(:),意图(输入):: arr真的 :: mysum整数 :: i,arrsizearrsize = 大小(arr)我的总和=0.0do i=1,arrsizemysum=mysum+arr(i)端部结束函数 mysum结束模块程序测试使用程序隐式无!这里没有声明 mysum,它来自模块...结束程序
2.内部过程 - 仅用于简短的简单过程或过程需要访问主机的变量.由于可以访问宿主变量,因此很容易出错.
程序测试隐式无!这里没有声明 a_sum,它在下面可见,包含...包含函数 mysum(arr)!implicit none 继承自程序真实,维度(:),意图(输入):: arr真的 :: mysum整数 :: i,arrsizearrsize = 大小(arr)我的总和=0.0do i=1,arrsizemysum=mysum+arr(i)端部结束函数 mysum结束程序
3.接口块 - 完全不推荐,你应该有一些特殊的理由使用它
函数 mysum(arr)!删除以节省空间结束函数 mysum程序测试隐式无界面函数 mysum(arr)真实,维度(:),意图(输入):: arr真的 :: mysum结束函数终端接口!没有 mysum 在那里声明!它在接口块中声明...结束程序
I am completely new to Fortran 90 and I am trying to understand how to pass an array to a function. I looked on the web and I could not find any clear and simple enough example, so I decided to post here.
I would like the function be able to work on an array of any length (the length of the array should not be one of the parameters of the functions).
I tried to write a simple example of a function that returns the sum of the elements of an array:
function mysum(arr)
implicit none
real, dimension(:), intent(in) :: arr
real :: mysum
integer :: i,arrsize
arrsize = size(arr)
mysum=0.0
do i=1,arrsize
mysum=mysum+arr(i)
enddo
end function mysum
program test
implicit none
real, dimension(4) :: a
real :: mysum,a_sum
call random_number(a)
print *,a
a_sum=mysum(a)
print *,a_sum
end program
When I try to compile, I get the following error:
array_test.f90:17.14:
real mysum,a_sum
1
Error: Procedure 'mysum' at (1) with assumed-shape dummy argument 'arr' must have an explicit interface
What is the problem with my program?
Assumed shape dummy arguments (those with (:)
) require explicit interface to the procedure to be available at the call site. That means the calling code must know how exactly the subroutine header looks like. See also Module calling an external procedure with implicit interface
That explicit interface can be provided in several ways
1. preferred - a module procedure
module procedures
implicit none
contains
function mysum(arr)
real, dimension(:), intent(in) :: arr
real :: mysum
integer :: i,arrsize
arrsize = size(arr)
mysum=0.0
do i=1,arrsize
mysum=mysum+arr(i)
enddo
end function mysum
end module
program test
use procedures
implicit none
!no mysum declared here, it comes from the module
...
end program
2. internal procedure - only for short simple procedures or if the procedure needs access to the host's variables. Because of the access to the host variables it is error-prone.
program test
implicit none
!no a_sum declared here, it is visible below contains
...
contains
function mysum(arr)
!implicit none inherited from the program
real, dimension(:), intent(in) :: arr
real :: mysum
integer :: i,arrsize
arrsize = size(arr)
mysum=0.0
do i=1,arrsize
mysum=mysum+arr(i)
enddo
end function mysum
end program
3. interface block - not recommended at all, you should have some particular reason to use it
function mysum(arr)
! removed to save space
end function mysum
program test
implicit none
interface
function mysum(arr)
real, dimension(:), intent(in) :: arr
real :: mysum
end function
end interface
!no mysum declared there
!it is declared in the interface block
...
end program
这篇关于具有假定形状虚拟参数的过程必须具有显式接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!