Fortran 中返回数组的函数

发布于 2024-09-25 15:59:18 字数 541 浏览 3 评论 0原文

据我了解,您可以从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

把回忆走一遍 2024-10-02 15:59:18

要定义返回数组的函数,请在函数内部包含函数声明,如下所示:

function polynomialMult(npts,x,y)
    integer npts
    double precision x(npts), results(npts + 1), y(npts,npts)

! Change the next line to whatever you want
    double precision, dimension(npts) :: polynomialMult

    polynomialMult =  x(1:npts) + 1

end function

您的声明

integer function polynomialMult(npts,x,y)

声明该函数返回一个整数。 一个整数,而不是整数数组。我不认为标准允许函数声明,例如:

integer, dimension(10) function polynomialMult(npts,x,y)

但我可能是错的。我总是使用上面向您展示的表格。

如果您有最新的 Fortran 编译器,您可以做一些聪明的事情,例如返回分配的数组。我建议你弄清楚数组语法。例如,您的语句:

polynomialMult =  x(1:npts) + 1

可以更简洁地编写:

polynomialMult =  x + 1

因为 Fortran 会将标量加法映射到您声明为仅包含 npts 元素的数组 x 的所有元素。

将数组的大小传递到子例程中非常符合 FORTRAN77,而且现在几乎总是不必要的。通常,您要么想要对数组中的每个元素进行操作(如数组语法示例中所示),要么应该让子程序计算出它正在处理的数组的大小。

To define a function which returns an array include the function declaration inside the function, like this:

function polynomialMult(npts,x,y)
    integer npts
    double precision x(npts), results(npts + 1), y(npts,npts)

! Change the next line to whatever you want
    double precision, dimension(npts) :: polynomialMult

    polynomialMult =  x(1:npts) + 1

end function

Your declaration

integer function polynomialMult(npts,x,y)

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:

integer, dimension(10) function polynomialMult(npts,x,y)

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:

polynomialMult =  x(1:npts) + 1

could more concisely be written:

polynomialMult =  x + 1

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.

悲欢浪云 2024-10-02 15:59:18

我同意前面的回复者的观点,即以下内容有效:

polynomialMult = x + 1

但是,如果不知道 polynomialMult 和 x 是数组,人们可能会认为它是标量运算。我更喜欢明显地这样做:

polynomialMult(:) = x(:) + 1

我什至坚持要求我小组中的编码员这样做。我不喜欢努力理解某人的代码——我希望他们在做什么是显而易见的。

I agree with the previous responder that the following works:

polynomialMult = x + 1

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:

polynomialMult(:) = x(:) + 1

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文