In Fortran 90, is it impossible to refer to previously declared variables in the declaration of a new variable declaration?

In Fortran, is it possible for variable declaration operators to refer to previously declared variables? For example, when I try to do the following:

PROGRAM test3 IMPLICIT NONE INTEGER :: a=2286 INTEGER :: b=a/3 WRITE(*,*) a, b END PROGRAM test3 

I get a compile time error message:

 test3.f90:5.16: INTEGER :: b=a/3 1 Error: Parameter 'a' at (1) has not been declared or is a variable, which does not reduce to a constant expression 

On the other hand, if I assign b to a / 2 in an expression other than declaration b , it compiles and works fine:

 PROGRAM test3 IMPLICIT NONE INTEGER :: a=2286 INTEGER :: b b=a/3 WRITE(*,*) a, b END PROGRAM test3 

which gives me the correct result:

 2286 762 

Why is it that previously declared variables cannot be included in declarations of declarations of new variables? Am I doing something wrong? Or is it just a "fact of Fortran's life"?

Thanks so much for your time!

+4
source share
3 answers

The error message is pretty explicit. Initializers using variable declarations must be persistent. In your example, a not a constant.

It should work as follows:

 PROGRAM test3 IMPLICIT NONE INTEGER, PARAMETER :: a=2286 INTEGER :: b=a/3 WRITE(*,*) a, b END PROGRAM test3 

because then a is a constant.

+6
source

Let me add one more thing: Such a variable is initialized in the main programs and parameters (well, you should initialize them for parameters), but this may surprise you with your behavior if you are too used to using it and starting to use it in routines and functions:

For example, most of us initially assumed that this program:

 program foo call bar call bar contains subroutine bar integer :: i=3 print '(A,I3)','At start of bar: i = ', i i = i + 1 print '(A,I3)','At end of bar: i = ', i end subroutine bar end program foo 

will print

 At start of bar: i = 3 At end of bar: i = 4 At start of bar: i = 3 At end of bar: i = 4 

--- but it is not. He is typing

 At start of bar: i = 3 At end of bar: i = 4 At start of bar: i = 4 At end of bar: i = 5 

This is for “historical reasons”, as it often happens when they present behavior that seems manifestly wrong. Initializing a variable when declaring essentially turns this:

 integer :: i 

in

 integer, save :: i = 3 

and initialization is only performed for the first time . This means that the second time the variable remembers this previous value (4) and increases it.

Therefore, my reason for writing this is mainly to warn you not too comfortable initializing variables during declaration. I recommend doing this for the parameters and in the main program (where you will not encounter this problem, since you only enter the main program once) and a little more.

+8
source

Add ", option" to ad "a". This will allow you to use the value of a in another declaration. It also means that the value of "variable" a cannot be changed.

+3
source

All Articles