4: Passing arrays; PARAMETER, DATA and FORMAT statements

4.1 Passing arrays

FORTRAN is designed to treat subroutines and function subprograms as independent. This means essentially that one of the main ways of communication between the subprogram and the calling program is through the argument list (the other is the COMMON declaration). Correct correspondence between the dummy arguments and the actual arguments (type, array size, number, etc.) is the responsibility of the program writer! FORTRAN does not normally check it. This is actually a very flexible feature, but can easily be a considerable source of programming errors.

	CALL SUBR1(NUM,X,Y,RESULT,IS)
	.
	END
	
	SUBROUTINE SUBR1(N,X,Y,R,I)
	.
	RETURN
	END

Check - there are five arguments in both call and subroutine.

Check - the first and last arguments are integer, the rest are real.

The next example shows the simplest form of passing an array. The arrays SIG and ARR are declared as the same size in the main program and the subroutine. The whole array is then passed:

	DIMENSION SIG(15),ARR(10,10)
	.
	CALL SUBR2(NUM,SIG,ARR,RESULT)
	.
	END

	SUBROUTINE SUBR2(NUM,SIG,ARRAY,RES)
	DIMENSION SIG(15),ARRAY(10,10)
	.
	RETURN
	END

This is straightforward, but rather inflexible since if we wanted to increase the size of the arrays in the main program we would have to remember to do this in the subroutine as well. FORTRAN allows the use of adjustable dimensions in subprograms - the size of the array is passed as an argument in the list:

	DIMENSION SIG(15),ARR(10,10)
	N1=15
	N2=10
	N3=10
	CALL SUBR3(NUM,SIG,N1,ARR,N2,N3,RESULT)
	.
	END
 
	SUBROUTINE SUBR3(NUM,SIG,NS,ARRAY,N,M,RES)
	DIMENSION SIG(NS),ARRAY(N,M)
	.

NS is the size of SIG and N & M the size of ARRAY. The main program must fix the size of the arrays it is not possible to have adjustable dimensions in the main program. However, the above example can be tidied-up using the PARAMETER statement, see below.

It is possible to live more dangerously, by effectively saying the array size is unknown. This is done using a * in the dimension. Thus:

	DIMENSION SIG(15),ARR(10,10)
	.
	N=15
	CALL SUBR4(NUM,SIG,ARR,N,RESULT)
	.
	END

	SUBROUTINE SUBR4(NUM,SIG,ARRAY,N,RES)
	DIMENSION SIG(*),ARRAY(N,*)
	.
	RETURN
	END

FORTRAN only allows the last dimension of an array to be 'unknown'.

4.2 The PARAMETER statement

	PARAMETER (name1=constant1,name2=constant2,..)

This is used right at the beginning of the program (or indeed subprogram), before any declarations, to set-up fixed constants including array sizes. When the code is compiled the value of the constant replaces the name of the constant at every occurrence in the program, and therefore name is not a variable whose value can be altered when the program is running.

	PARAMETER (N1=10,N2=15,N3=15,PI=3.14159265)
	DIMENSION SIG(N1),ARR(N2,N3)
	.
	AREA=PI*RAD**2
	.
	CALL SUBR3(NUM,SIG,N1,ARR,N2,N3,RES)
	.
	END

4.3 The DATA statement

	DATA name1,name2/constants/,name3/constants/...

The DATA statement is simliar to the PARAMETER statement in that it gives values to named constants. However, it does not replace the name of the constant during compilation, it merely assigns values. Thus

	DATA PI/3.14159265/

is equivalent to

	PI=3.14159265

DATA statements are used at the beginning of the program after the declarations to assign initial values to variables.

4.4 FORMAT of the output

The basic output statement for writing results to the screen has been PRINT*. This has left the decision, as to where to put the result, up to the computer! We can direct, or format, the output on the screen more precisely by replacing the * with format specifiers or with a label which refers to a FORMAT statement.

	PRINT*,'THE ANSWER IS ',RES

	PRINT'('' THE ANSWER IS '',F8.4)',RES

	PRINT 10,RES
 10     FORMAT(' THE ANSWER IS ',F8.4)

The above three print statements all output the same information. The first is called a list-directed output. The second and third methods are format-directed output and are identical. If the value of RES is 100.5648964 then the last two statements will produce output:

	THE ANSWER IS 100.5649		followed by carriage return

The format contains a number of fields separated by commas. The F8.4 is specifying that the output is to be real (F for floating), that the total length of the field for RES is 8 characters (including the decimal point and sign) and that 4 characters are to appear after the decimal point. This leaves 3 character spaces before the decimal point. The field is right justified. The brackets are essential and the closing of the bracket ends the record with a carriage return on the output line. Therefore each time PRINT 10,RES is executed the output would start on a new line.

In general the real field specifier is:

		numberFwidth.digits

where number is the number of times the field is repeated, width is the total width of the field in characters (including sign and decimal point) and digits is the number of decimal places. The field is right justified.

For integers it is:

		numberIwidth

with the same meanings as for the real field specifier.

for spaces the specifier is:

	numberX

This produces the number number of blanks on the printed line. When using format-directed output the first character that is printed must be a blank.

      PRINT 10,I,J
  10  FORMAT(1X,2I6)

      PRINT 20,I,J
  20  FORMAT(' THE VALUES OF I & J ARE ',2I6)

This would output (if I=4 and J=6):

	     4     6
	THE VALUES OF I & J ARE      4     6

Forgetting the blank in the second case would lose you the first character 'T'. Note also the integer numbers are right justified in their fields.

If you want to force a carriage return & new line in the output then / is used:

      PRINT 30,I,J,RES
  30  FORMAT(' I & J ARE: ',I2,4X,I2,/,' THE RESULT IS ',F10.4)

would output the following if I=1, J=5, and RES = -34.56546212:

	I & J ARE:  1     5
	THE RESULT IS   -34.5655

If the number to be output is too big for the size of field you've specified, the field is sometimes filled with ****, and/or an error message is printed. In the example above, if F7.4 was used instead of F10.4, there is no space for the sign.