Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaration and initialization of local variables in Fortran PURE FUNCTIONs

Tags:

fortran

I have a function that looks like this:

PURE FUNCTION simulate(initial_state, time_specification)
        TYPE(ocean), INTENT(IN) :: initial_state
        TYPE(simulation_time), INTENT(IN) :: time_specification
        TYPE(ocean) :: simulate
        REAL :: t = 0.0      
        ! etc
END FUNCTION simulate

gfortran 4.8.1 informs me that

 REAL :: t = 0.0
                1
Error: Initialization of variable at (1) is not allowed in a PURE procedure

As I understand it, I should be able to use local variables within pure functions as long as they do not have the SAVE attribute. So what am I doing wrong?

like image 352
hertzsprung Avatar asked Jan 28 '14 22:01

hertzsprung


People also ask

Does Fortran initialize variables to zero?

Fortran variables are NOT by default initialized to anything. I believe C++ is the same. See -finit-real .

Do local variables need to be initialized?

Local Variables. Local variables must be initialized before use, as they don't have a default value and the compiler won't let us use an uninitialized value.


1 Answers

Under modern Fortran initialization implies SAVE. From F2008 5.2.3

Explicit initialization of a variable that is not in a common block implies the SAVE attribute, which may be confirmed by explicit specification.

You can use local variables, but just

real t
t = 0

which isn't initialization.

like image 93
francescalus Avatar answered Oct 01 '22 15:10

francescalus