Is a MODULE better than a COMMON block.
Almost always yes. The only reasons to use COMMON blocks are if you expect to use your program on a computer with only a FORTRAN 77 compiler (they still exist), or if it is very important that you control the order in which your data is stored in memory.
What does "COMMON/CONTROL/ A, B, C" do to the values of A, B, C?
It doesn't do anything to the values. It just establishes a place in memory for the values to be stored and retrieved. Every subroutine and function in your program that contains the line "COMMON/CONTROL/ A, B, C" will agree that operations using variables A, B, or C will get numbers from or put numbers in the same appropriate location in memory. That is, they agree that a reference to "A" means the same in all routines with common CONTROL, etc.. To give A, B, or C a value, you have two options. You can assign values at compilation time with a BLOCK DATA routine:
You can also assign and use values with executable statements. Say subroutines SUB1 and SUB2 contain this common block. If SUB1 contains the lines "A=1.0", and "B=2.0", and SUB2 has the line "C=A+B", then the value of C after this line in SUB2 is executed is 3.0.
BLOCK DATA ABCVAL COMMON/CONTROL/ A, B, C DATA A,B,C / 1.0, 2.0, 3.0/ END
Do the declarations of REAL and INTEGER have to come after the COMMON statement?
What's the use of a blank common as opposed to a common with a name?
The major use is in cutting the size of the of the executable file produced by "f77" (usually a.out). It won't seem like a big deal to you now, but in applications containing many arrays with thousands to millions of elements each, this can make a huge difference in the amount of disk space you soak up. A secondary use is that, when the program starts, the space finally allocated to blank common in memory is at the very end of the program. If you are tricky enough, you can take advantage of this to dynamically extend the size of a single array in blank common as your space requirements grow during execution.
When is the common command not used correctly in a Fortran Statement?
You're asking for quite a bit here. There are lots of ways to introduce errors. The most obvious is to try inserting a common in the midst of executable statements. It belongs up with DIMENSIONs, PARAMETER's, SAVE's, and the rest of the non-executables. A more subtle problem is the use of multiple COMMON statements for blank common or the same named common. Fortran will accept the following program:
program testcom common a,b common c,d a=1.0 b=2.0 c=3.0 d=4.0 call sub1 stop end subroutine sub1 common c,d print *, c, d return end
However, the values printed out are 1.0 and 2.0. The second common statement in the main program just tacks the variables "c" and "d" onto blank common after "b". The two commons together are equivalent to "common a,b,c,d". In the subroutine you are saying that "c" is the first element in blank common, so Fortran associates "c" in "sub1" with the same address in memory as assigned to "a" in the main program. Also recall that mixing reals and integers (common/blk1/a,b,i,c) when the reals are double precision may cause errors on some machines, and is not a good idea on any 32 bit machine.
How do you use a BLOCKDATA (where you put it, etc)?
Use it like a subroutine, but instead of starting with a "SUBROUTINE" statement, start with a "BLOCKDATA" statement. End it with an "END", but don't bother with the "RETURN". You can put it anywhere in the program after the "END" of another subprogram. Take a look at the use of BLOCKDATA in fall.f.
If you use a BLOCK DATA statement to initialize a common block, do you lose the advantage of a smaller executable file?
No, and Yes. You don't lose anything because the disk space advantage was already gone when you decided to use the named common blocks rather than blank common. BLOCK DATA will only assign values to contents of named common blocks. The disk space decision takes place when you choose to place variables in named common or blank common. Please remember that you can have lots of different named common blocks, but there is only one blank common block.
Why would you use BLOCKDATA?
When doing scientific and engineering calculations, there are lots of constants that you may need. The values of Pi, g, Plank's constant are simple ones. Often there are hundreds to thousands of constants that feed into Equations of State and calculations of physical properties like fluid viscosities, conductivities, and so on. These really are constants. Numbers that are needed for calculations, but that you do not want to calculate yourself. Generally, once imbedded in a code you won't change them for many years (or the rest of your life). To save computer time you want to preload the values of the constants with a DATA statement.
You will need many of these constants in several subroutines, so you may choose to transmit them with a COMMON. At this point Fortran backs you into a corner and forces you to create a BLOCKDATA subroutine for DATA statements defining values for any variables in COMMON blocks. For example, if I am going to give access to the values for Pi and g in:
and I want to define the values with a DATA statement, then I have to include:
Maintained by John Mahaffy : email@example.com