从函数中返回一个数组并将其存储在主程序中 [英] Return an array from a function and store it in the main program
问题描述
这是主程序:
PROGRAM integration
EXTERNAL funct
DOUBLE PRECISION funct, a , b, sum, h
INTEGER n, i
REAL s
PARAMETER (a = 0, b = 10, n = 200)
h = (b-a)/n
sum = 0.0
DO i = 1, n
sum = sum+funct(i*h+a)
END DO
sum = h*(sum-0.5*(funct(a)+funct(b)))
PRINT *,sum
CONTAINS
END
下面是函数funct(x)
And below is the Function funct(x)
DOUBLE PRECISION FUNCTION funct(x)
IMPLICIT NONE
DOUBLE PRECISION x
INTEGER K
Do k = 1,10
funct = x ** 2 * k
End Do
PRINT *, 'Value of funct is', funct
RETURN
END
我希望主程序中的 'Sum'
打印函数 funct(x)
中 10 个不同 k 值的 10 个不同总和.
I would like the 'Sum'
in the Main Program to print 10 different sums over 10 different values of k in Function funct(x)
.
我已经尝试过上面的程序,但它只是编译了 Funct()
的最后一个值,而不是总共 10 个不同的值.
I have tried the above program but it just compiles the last value of Funct()
instead of 10 different values in sum.
推荐答案
数组结果需要显式接口.您还需要使用 dimension
语句将 funct
和 sum
调整为实际上是数组.使用显式接口需要 Fortran 90+(感谢@francescalus 和@VladimirF 的提示)并且非常乏味:
Array results require an explicit interface. You would also need to adjust funct
and sum
to actually be arrays using the dimension
statement. Using an explicit interface requires Fortran 90+ (thanks for the hints by @francescalus and @VladimirF) and is quite tedious:
PROGRAM integration
INTERFACE funct
FUNCTION funct(x) result(r)
IMPLICIT NONE
DOUBLE PRECISION r
DIMENSION r( 10 )
DOUBLE PRECISION x
END FUNCTION
END INTERFACE
DOUBLE PRECISION a , b, sum, h
DIMENSION sum( 10)
INTEGER n, i
PARAMETER (a = 0, b = 10, n = 200)
h = (b-a)/n
sum = 0.0
DO i = 1, n
sum = sum+funct(i*h+a)
END DO
sum = h*(sum-0.5*(funct(a)+funct(b)))
PRINT *,sum
END
FUNCTION funct(x)
IMPLICIT NONE
DOUBLE PRECISION funct
DIMENSION funct( 10)
DOUBLE PRECISION x
INTEGER K
Do k = 1,10
funct(k) = x ** 2 * k
End Do
PRINT *, 'Value of funct is', funct
RETURN
END
如果可以,您应该切换到更现代的标准,例如 Fortran 90+,并使用 modules
.它们自动提供接口,这使得代码更简单.
If you can, you should switch to a more modern Standard such as Fortran 90+, and use modules
. These provide interfaces automatically, which makes the code much simpler.
或者,您可以将 k
上的循环从函数中取出,并按元素执行求和.这将是有效的 FORTRAN 77:
Alternatively, you could take the loop over k
out of the function, and perform the sum element-wise. This would be valid FORTRAN 77:
PROGRAM integration
c ...
DIMENSION sum( 10)
c ...
INTEGER K
c ...
DO i = 1, n
Do k = 1,10
sum(k)= sum(k)+funct(i*h+a, k)
End Do
END DO
c ...
请注意,我将 k
传递给函数.需要相应调整:
Notice that I pass k
to the function. It needs to be adjusted accordingly:
DOUBLE PRECISION FUNCTION funct(x,k)
IMPLICIT NONE
DOUBLE PRECISION x
INTEGER K
funct = x ** 2 * k
PRINT *, 'Value of funct is', funct
RETURN
END
这个版本只返回一个标量并填充主程序中的数组.
This version just returns a scalar and fills the array in the main program.
除此之外,我不确定使用名为 sum
的变量是否明智.有一个同名的内在函数.这可能会导致一些混乱...
Apart from that I'm not sure it is wise to use a variable called sum
. There is an intrinsic function with the same name. This could lead to some confusion...
这篇关于从函数中返回一个数组并将其存储在主程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!