Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference when declaring a variable with a double colon

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.

like image 221
Exascale Avatar asked Mar 03 '14 18:03

Exascale


People also ask

What is double colon used for?

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)

What does double colon in python mean?

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).

What does colon mean in Fortran?

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.

What is the name of double colon?

Master C and Embedded C Programming- Learn as you go The prepended double colon is also known as the scope resolution operator.


1 Answers

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.

like image 116
Rob Avatar answered Nov 09 '22 22:11

Rob