- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I get error #6008 when I compile this.
How then should I initialize these arrays?
except by copying arrays from some other locations that are not in the module.
Actually I dont understand why DATA statements would be a problem here.
See attached routine.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
To allow this to compile reliably, you want the compiler to see the source code for the module before it sees the source code for things that `use` the module.
Note that DATA specifies initialization. Initialization is something that conceptually happens before the program starts executing.
Bar some exceptions not relevant here, when something is defined in a module the "totality" of its definition is provided in the module. The initialization value of something is part of its definition (as is the implied SAVE status that DATA gives to the thing being initialized, when that's applicable) - after all a thing can only be initialized once.
With your example code you've got that definition split across two program units - in the module you define the type and size of mxcom and ncom, in the main program you provide the initialization details.
To highlight - consider this erroneous snippet:
MODULE m IMPLICIT NONE INTEGER :: some_variable END MODULE m SUBROUTINE a USE m IMPLICIT NONE DATA some_variable/1/ END SUBROUTINE a SUBROUTINE b USE m IMPLICIT NONE DATA some_variable/2/ END SUBROUTINE b
Before a program that holds this code starts executing, is the initial value of some_variable 1 or 2? Conceptually this doesn't work - hence it is illegal.
Note that initialization (happens before the program starts executing) is different from "an assignment that happens early on in the execution of a program or subroutine". If your example was rewritten:
module sud003 IMPLICIT NONE integer mxcom(9) integer ncom(9) end module use sud003 IMPLICIT NONE mxcom = 20 ! Assign the value 20 to all elements. ncom = 0 ! Assign the value 0 to all elements. call newbox(1) print *,"DONE" read(*,*) end program
then you shouldn't get any complaints for that part of the code.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Fine except:
1) I was under the impression the DATA statements are supposed to initialize everything BEFORE
execution begins, and
2) The whole point of putting the DATA statement in the MAIN program was to take care of all that
initialization ahead of time, and finally:
3) The MODULES are supposed to be compiled first anyway.
It isnt clear to me what would happen IF i put the data statements in the module statements.
Can Steve give his input here?
Please?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Fortran has a rule that prohibits changing attributes of an identifier made accessible by use or host association. (There are a couple of exceptions to this not relevant to your question.) DATA is specifying an initialization attribute of the variable, it isn't an executable construct. Therefore if you want to use DATA for these variables you must place the DATA declarations in the same module where the variable is declared. (Or you can use an initializer in the declaration itself rather than DATA.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
OK, that makes sense -
I normally would put the MODULES in separate files, but I wanted to keep this example as simple as I could.
So I should put my DATA statements in the same file as the MODULE that has the variables.
Thanks ! !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Not only the same file, but in the same module (or even right after the declaration of the variable).

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page