18

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.

nbro
  • 15,395
  • 32
  • 113
  • 196
Exascale
  • 929
  • 1
  • 7
  • 17

1 Answers1

19

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.

nbro
  • 15,395
  • 32
  • 113
  • 196
Rob
  • 3,315
  • 1
  • 24
  • 35
  • That's exactly it, the default save behavior was unexpected. Looking at the ifort documentation again answers all my questions. The **:: can be excluded** and save is default for **Non scalar local variables**. My variable was an array which leads me to believe that was the issue if arrays are not scalar. – Exascale Mar 03 '14 at 19:02
  • 3
    It may be worth explicitly saying that initialization also requires the `::` as well as implying `SAVE`. – francescalus Mar 03 '14 at 20:53