Question

When declaring variables is there a difference when using a double colon?

real(8) :: a
real(8) b

Both of these apparently do the same thing. Is there any difference between these besides style?

I know we can initialize variables and add attributes as follows

real(8), intent(in), parameter :: a = 4, b = 2

but besides that, is there any difference when just declaring a plain old real or integer with no attributes and not initializing?

Also, does this have anything to do with the SAVE attribute? A while back in some of my code was behaving unexpectedly and I was saving the results of a function between calls, which forced me to explicitly set the variable to zero each time the function was called, even though the SAVE attribute was not set by me.

Était-ce utile?

La solution

In your first example the :: is not needed and can be omitted. The general syntax is:

type-spec [ [,attr-spec]... :: ] entities

In your first case:

type-spec: real(8)
entities: a and b

The square brackets in the syntax definition mean that that part is optional. If however you specify an attr-spec (like intent(in) or parameter), then the :: is required. Specifically:

[ [, attr-spec] :: ]

means that the :: is optional and attr-spec is optional, but if you give and attr-spec you MUST also give the ::.

I suspect people just get into the habit of providing the :: for every declaration.

In the example:

real :: a=4.5

The =4.5 forces a to be SAVEed which may cover the second part of your question.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top