直到最近我决定学习自由格式的 Fortran 之前,我都在 Matlab 中完成了所有的编程。我将 gfortran 编译器与 Cygwin 一起使用。我有兴趣编写可以将数组作为输入的函数,对其进行计算,然后将新数组传回。我从不认为这是一项艰巨的任务,但事实证明它至少对我来说是一项艰巨的任务。这是我的简单测试代码:
PROGRAM RETURN_ARRAY
! Description: This program is used to test a function that returns
! an array.
REAL*8 :: NROOT
PRINT *, NROOT(2, [1.0D0, 4.0D0, 9.0D0, 16.0D0, 25.0D0])
END PROGRAM RETURN_ARRAY
FUNCTION NROOT(N, X) RESULT(Y)
! Description: This function calculates the Nth root of a real number
! or array of real numbers.
!
! Inputs: N - desired Nth root
! X - real number or array of real numbers to take Nth root of
!
! Outputs: Y - Nth root of real number or array of real numbers, X
INTEGER, INTENT(IN) :: N
REAL*8, INTENT(IN) :: X(:)
REAL*8 :: P
REAL*8 :: Y(SIZE(X))
P = 1.0D0/N
Y = X**P
END FUNCTION NROOT
`我编译这个程序和内部函数如下:
gfortran RETURN_ARRAY.f90 -o MAIN.exe
程序编译没有错误。但是,当我尝试运行该程序时,我在终端中得到以下输出:
Segmentation fault (core dumped)
我将不胜感激解决此问题的任何帮助。提前致谢。