- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 Fortran 90 完全陌生,我试图了解如何将数组传递给函数。我在网上查了一下,找不到任何足够清晰和简单的例子,所以我决定在这里发布。
我希望该函数能够处理任意长度的数组(数组的长度不应该是函数的参数之一)。
我尝试编写一个返回数组元素之和的函数的简单示例:
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
当我尝试编译时,出现以下错误:
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
我的程序有什么问题?
最佳答案
假定的形状虚拟参数(带有 (:)
的参数)需要调用站点上可用的过程的显式接口(interface)。这意味着调用代码必须知道子例程 header 到底是什么样子。另请参阅Module calling an external procedure with implicit interface
可以通过多种方式提供显式接口(interface)
1.首选 - 模块过程
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.内部过程 - 仅适用于简短的过程或过程需要访问主机的变量。由于访问主机变量,因此很容易出错。
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.界面 block - 完全不推荐,您应该有一些特殊的理由来使用它
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
关于function - 具有假定形状虚拟参数的过程必须具有显式接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42766530/
关于 Stylus 的一大优点是它允许您定义可用于自定义结果输出配置的变量。 例如, // my-html-object.styl $my-html-object-color = red $my-ht
我是 C++ 的新手,只有一个 C++ 的小头文件,里面有一个简单的结构。 PGNFinder.h: #ifndef PGNFINDER_H #define PGNFINDER_H struct Fi
我正在为 Windows 编写一段 C++ 代码,需要查询进程及其每个单独线程的计时。 为了进行必要的系统调用,我需要进程及其每个线程的句柄。我正在使用 getCurrentProcess 和 get
我正在尝试从数据库中检索数据,但将其限制为每个 View 的特定数量的项目。但相反,我得到了上述错误。我创建了以下函数来检索数据: //function to display jobs functio
我有一个用于我的 C++ 应用程序的 settings.ini 文件,但是我的一个用户 Windows\Temp 坏了,导致它对他来说一团糟。所以我打算将它移到他的用户文件夹中。所以我想在全局变量部分
This post says如果您的数据大于 2GB,您需要 64 位系统用于 MongoDB。 在我的本地机器上,运行 32 位 Mac OS X Leopard 的 64 位 Intel C2D,
Thymeleaf tutorial code 这个 SeedStarter 是如何实例化的?请参阅方法主体显示与对象 SeedStarter 的交互。那么它首先是如何实例化的? 由于这个项目是一个教
因此,在这个相当大的源文件中,我有以下部分,假设在函数 foo 中,从 main 调用: FILE *logfile = NULL if (log_engabled) { char fname
出于某种原因,我坚持使用 List 作为集合类型的模型类,我想在客户端使用该模型。然而,GWT 当然无法序列化 java.util.List。但是,此模型中 List 的所有实现都基于 ArrayLi
如何使用 JavaScript 解析此 CSV? 1363085391,42.890000000000,5.432200000000 1363088879,47.570000000000,4.9818
我正在使用名为 paypal_class 的 codeigniter 库。一切都很好,但是在验证 ipn 函数时我得到了这个错误。 使用未定义的常量主机 - 假定为“主机”,这一定是邮件未发送给用户的
这个问题已经有答案了: Forcing GCC to compile .cpp file as C (2 个回答) 已关闭 10 年前。 我有一个 C++ 项目 [IDE = codelite],它尝
这个问题在这里已经有了答案: Sorting a vector of custom objects (14 个答案) 关闭 6 年前。 我创建了一个 vector vector ,我想根据我定义的参
我是一名优秀的程序员,十分优秀!