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.
Use the double colon operator (::) to qualify a C++ member function, a top level function, or a variable with global scope with: An overloaded name (same name used with different argument types) An ambiguous name (same name used in different classes)
Answer: The double colon is a special case in Python's extended slicing feature. The extended slicing notation string[start:stop:step] uses three arguments start , stop , and step to carve out a subsequence. It accesses every step -th element between indices start (included) and stop (excluded).
Fortran90 and up allow you to access a single array value given an index, and access a subarray given a range of indices separated by a colon.
Master C and Embedded C Programming- Learn as you go The prepended double colon is also known as the scope resolution operator.
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 SAVE
ed which may cover the second part of your question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With