user4143737
user4143737

Reputation: 11

Declaring parameters through input arguments in FORTRAN

I am trying to write a fortran routine where I declare arrays whose length comes from operations made upon the input parameters.

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

Right now, I have an error coming from the the 5th line Parameter 'N' at (1) has not been declared or is a variable, which does not reduce to a constant expression

Upvotes: 1

Views: 76

Answers (1)

Alexander Vogt
Alexander Vogt

Reputation: 18118

N is not known at compile time, so you cannot use it to initialize a parameter. Instead, use N directly to declare A and B:

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

Upvotes: 2

Related Questions