20 Common and data statements
Chapter 20 Common and Data Statements
Program ANALYS has ‘calls’ to the subroutines PASS1 and COMPUT, and PASS1 has a ‘call’ to subroutine PASS2. This is summarised diagrammatically below:–
The common block B is safe. It is in the main program, so we never return to a routine which does not contain it. But, although subroutines PASS1 and PASS2 can use the common block A, when eventually PASS1 completes its action and returns control to the main routine, i.e. to program ANALYS, the entire com - mon block A becomes undefined, so that if the program should call PASS1 again, those values would be unavailable.
The SAVE statement is a declarative statement which may be used to retain the values in the common block;
SUBROUTINE PASS1
COMMON /A/B(100),C(25),L,J,K SAVE /A/
would have the effect of retaining the values of any variables in common block A when we return to the main program. If we used PASS1 again, the values would be available.
In fact, SAVE is more general still, we may use it to retain the values of arrays and variables when a return takes place; otherwise the values would become undefined.
DIMENSION X(100) SAVE X,A,B
would retain the values of the array X and the two simple variables.
It is sensible to put variables that are related into the same common block, and to choose a meaningful name.
COMMON is also often used with another important declaration — the DATA statement. The DATA statement is used to provide initial values for variables and arrays; e.g.
DATA COEFF1 /10.382/
This value of COEFF1 is set up at compile time. It is even more flexible, since we can initialise whole arrays in a very simple way;
DIMENSION X(100) DATA X/100*0.0/
This is a form of the implied DO loop. DATA allows us to initialise arrays or simple variables. Variables in DATA statements are only initialised once — either when the program is compiled, or when the program is loaded, depending
on the system that you work on. Thus, the DATA statement is not executable, unlike the assignment statement. You may initialise character strings thus
CHARACTER STR1*6, STR2*3 DATA STR1/’ABCDEF’/ DATA STR2/’ABC’/
Now, you might think that it would be very useful to combine DATA and COMMON statements together, so that you could do something like:
COMMON /COEFF1/COEFF1 DATA COEFF1/10.382/
but this can only be done in one special program segment — the BLOCK DATA sub-program, and only for named COMMON:
BLOCK DATA SETUP COMMON /CAT/X,Y,Z COMMON /DOG/L DATA X,Y/1.0,2.0/ DATA Z,L/201.23,3/ END
Note that we can initialise any variables that occur in a named COMMON statement, but only in this special sub-program which contains no RETURN or STOP statement. This routine is never executed. It is never CALLed or refer - enced directly in the rest of the program. It is used only at compile time to initialise, and it is not used at run time at all. You may have several BLOCK DATA sub-programs, each with a different name. If you have only one, it need not have a name at all.
COMMON and SAVE are declarative statements, and therefore join the other declaratives at the ‘beginning’ of the sub-program. DATA belongs to a post- declaration, pre-execution limbo, and may be sandwiched between the two groups. However, it does not matter where the DATA statement comes (pro - vided it is not among the declarative specifications). The DATA causes initialisation at the compile stage, as described above, and its position within the execution statements is irrelevant.
Summary
• Common blocks allow data to be shared between sub-programs.
• Character data must be in its own common block.
• The COMMON declaration merely sets aside a section of memory with an identifier (the COMMON block name). You may access this section of memory however you wish in other sub-programs, through the COMMON block name.
• On return to a sub-program, the contents of a common block may become undefined. Blank common never becomes undefined within a program. The SAVE declaration will safeguard named COMMON.
• Data may be initialised in DATA statements; these are not executable state - ments, but are set up at ‘load’ time.
• You may not use the DATA statement to set up the contents of a COMMON block, except in the BLOCK DATA sub-program.
Problems
1. Write a program that uses a DATA statement to initialise an integer variable. This integer variable must be in a common block. The program should contain a main program, block data sub-program, and two subroutines. The main pro - gram should print out the value of the integer variable initialised in the DATA statement. Each of the two subroutines should also print out the value of this same variable. Each subroutine just prints out the value of the integer variable. The main program should
• print out the value of the variable
• call subroutine 1
• print out the value of the variable again.
• call subroutine 2
• print out the value of the variable again
What do you notice about the values printed out? Alter the variable to be of type character, and run the program again. You will need to alter the DATA statement also. What do you notice now?
2. Some of the previous programs, subroutines and functions could benefit from using the DATA statement to initialise some information. For example, the co - efficients of the Bessel function in the problems in Chapter 12 could usefully be initialised in this way (either as simple variables, or as vectors in an implied DO). Do this.