通过FORTRAN中的输入参数声明参数

时间:2014-10-15 01:30:27

标签: fortran gfortran

我正在尝试编写一个fortran例程,其中我声明了数组,其长度来自对输入参数进行的操作。

subroutine my_program(N, A,B,m)

implicit none
integer, intent(in) :: N
integer, parameter :: minA = 1, maxA = N
integer, parameter :: minB = 0, maxB = N-1
double precision, intent(out) :: max,A(minA:maxA),B(minB:maxB)

A = 0.d0
B = 1.d0
m = maxA*maxB-minA*minB

end subroutine my_program

现在,我从第5行Parameter 'N' at (1) has not been declared or is a variable, which does not reduce to a constant expression

发出错误

1 个答案:

答案 0 :(得分:2)

编译时不知道

N,因此您无法使用它来初始化parameter。相反,请直接使用N来声明AB

subroutine my_program(N, A, B, m)
  implicit none
  integer, intent(in)           :: N
  double precision, intent(out) :: m, A(1:N), B(0:N-1)

  integer                       :: minA, maxA 
  integer                       :: minB, maxB

  minA = 1 ; maxA = N
  minB = 0 ; maxB = N-1

  A = 0.d0
  B = 1.d0
  m = maxA*maxB - minA*minB
end subroutine my_program