Fortran 中返回数组的函数
据我了解,您可以从 Fortran 中的函数返回一个数组,但由于某种原因,我的代码只返回我要求它返回的数组中的第一个值。这就是函数:
function polynomialMult(npts,x,y)
integer npts
double precision x(npts), results(npts + 1), y(npts,npts)
polynomialMult = x(1:npts) + 1
end function
这就是我
C(1:numPoints) = polynomialMult(numPoints,x,f)
print *, C(1:numPoints)`
现在调用它的地方,它没有做任何有用的事情,因为我试图在编写逻辑之前理解语法。我看到了一些关于指定函数类型的内容,但是当我编写
integer function polynomialMult(npts,x,y)
或执行其他操作时,我收到编译错误。
It is my understanding that you can return an array from a function in Fortran, but for some reason my code is only returning the first value in the array I am asking it to return. This is the function:
function polynomialMult(npts,x,y)
integer npts
double precision x(npts), results(npts + 1), y(npts,npts)
polynomialMult = x(1:npts) + 1
end function
and this is where I'm calling it
C(1:numPoints) = polynomialMult(numPoints,x,f)
print *, C(1:numPoints)`
right now it doesn't do anything useful because I am trying to understand the syntax before I write the logic. I saw some stuff about specifying types for functions, but when I write
integer function polynomialMult(npts,x,y)
or whatever I get a compilation error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要定义返回数组的函数,请在函数内部包含函数声明,如下所示:
您的声明
声明该函数返回一个整数。 一个整数,而不是整数数组。我不认为标准允许函数声明,例如:
但我可能是错的。我总是使用上面向您展示的表格。
如果您有最新的 Fortran 编译器,您可以做一些聪明的事情,例如返回分配的数组。我建议你弄清楚数组语法。例如,您的语句:
可以更简洁地编写:
因为 Fortran 会将标量加法映射到您声明为仅包含
npts
元素的数组 x 的所有元素。将数组的大小传递到子例程中非常符合 FORTRAN77,而且现在几乎总是不必要的。通常,您要么想要对数组中的每个元素进行操作(如数组语法示例中所示),要么应该让子程序计算出它正在处理的数组的大小。
To define a function which returns an array include the function declaration inside the function, like this:
Your declaration
declares that the function returns an integer. An integer, not an array of integers. I don't think the standard allows function declarations such as:
but I could be wrong. I always use the form I showed you above.
If you have an up to date Fortran compiler you can do clever things such as return an allocated array. And I suggest you figure out array syntax. For example, your statement:
could more concisely be written:
since Fortran will map the scalar addition to all elements of the array x which you have declared to have only
npts
elements.Passing the sizes of arrays into subroutines is very FORTRAN77 and almost always unnecessary now. Generally you either want to operate on every element in an array (as in the array syntax example) or you should let the subprogram figure out the size of the array it is dealing with.
我同意前面的回复者的观点,即以下内容有效:
但是,如果不知道 polynomialMult 和 x 是数组,人们可能会认为它是标量运算。我更喜欢明显地这样做:
我什至坚持要求我小组中的编码员这样做。我不喜欢努力理解某人的代码——我希望他们在做什么是显而易见的。
I agree with the previous responder that the following works:
However, without knowing that polynomialMult and x are arrays, one might assume it is a scalar operation. I prefer to be obvious and do it this way:
I have even insisted that the coders in my group do it this way. I don't like to work hard to understand someone's code--I want it to be obvious what they are doing.