具有假定形状伪参数的过程必须具有明确的接口 [英] Procedure with assumed-shape dummy argument must have an explicit interface
问题描述
我对Fortran 90来说是全新的,我正试图理解如何将数组传递给函数。我查看了网络,但找不到任何清晰和简单的示例,因此我决定在此处发帖。
我希望该功能能够在 的任何长度的数组(长度不应该是函数的参数之一)。
我试图写一个简单的返回数组元素总和的函数示例:
函数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
结束函数mysum
程序测试
隐式无
实数,维(4):: a
real :: mysum,a_sum
调用random_number(a)
print *,a
a_sum = mysum (a)
print *,a_sum
结束程序
当我尝试编译,我得到了f orlowing error:
array_test.f90:17.14:
真正的mysum,a_sum
1
错误:(1)处假设形状伪参数'arr'的程序'mysum'必须具有显式接口
我的程序有什么问题?
$ c>(:))需要显式接口来使程序在呼叫站点可用。这意味着调用代码必须知道子例程标题的样子。另请参阅使用隐式接口调用外部过程的模块
该显式接口可以以多种方式提供
1。
首选 - 模块过程
模块过程
隐式无
包含
函数mysum(arr)
实数,维(:),intent(in):: arr
real :: mysum
integer :: i,arrsize
arrsize = size(arr)
mysum = 0.0
do i = 1,arrsize
mysum = mysum + arr(i)
enddo
结束函数mysum
结束模块
程序测试
使用过程
隐含无
!无mysum在这里声明,它来自模块
...
结束程序
2。
内部程序 - 仅适用于简单的程序或程序需要访问主机变量。由于访问主机变量,它很容易出错。
程序测试
隐式无
!在这里没有声明a_sum,它在下面可见包含
...
包含
函数mysum(arr)
!隐式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
结束函数mysum
结束程序
<3> 3。
接口块 - 不推荐使用,您应该有一些特别的理由来使用它
函数mysum(arr)
!删除以节省空间
结束函数mysum
程序测试
隐式无
接口
函数mysum(arr)
real ,维(:),intent(in):: arr
real :: 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屋!