1.5.8 Data Delivery to Subroutines

Data delivery using arguments often utilize a technique suited for the memory sequence of arrays stated in the previous section. In IFLAB, RFLIB< and VRLIB of MATH1, there are always arguments (such as jx ) for using components of one-dimensional arrays in a stepwise manner so that 1-D arrays can be processed using the same subroutines or functions for multi-dimensional arrays. To understand how these routines work, let us look at the following program.

      REAL X(100)

      ......

      CALL SUM(X, 100, 1, XSUM)

      ......

      END



      SUBROUTINE SUM(X, N, JX, XSUM)

      REAL X(*)



      XSUM = 0.

      DO 100 I=1, JX*(N-1)+1, JX

        XSUM = XSUM + X(I)

  100 CONTINUE

      END

In this subroutine, the sum x of the 1-D array is calculated and returned as xsum. If the same subroutine is used on a 2-D array y

      REAL Y(10,10) 

      ........

      CALL SUM(Y(2,1), 10, 10, YSUM)

      ......

      END

the subroutine sum assigns the memory area of the 2-D array beginning with element Y(2,1) to the 1-D array x in the following manner.



Y(2,1) Y(3,1) $ \cdots $ Y(10,1) Y(1,2) Y(2,2) $ \cdots $ Y(10,2) Y(1,3) Y(2,3) $ \cdots $
X(1) X(2) $ \cdots $ X(9) X(10) X(11) $ \cdots $ X(19) X(20) X(21) $ \cdots $


The, taking the  sum of the every 10th element 10 (JX) of the 1-D array x will then be equal to taking the sum of the second subscript element for the 2-D array y, Y(2,1), Y(2,2), . . . , Y(2,10).

It should be noted here that in this subroutine, the length of array x is unknown, and that the data are simply treated as a sequence of data beginning with X(1).  When the array is delivered to the subroutine, only the address of the array in the main program is copied over to the subroutine, and not the contents of the array. The subroutine carries out the processing based on the delivered addresses. If this is kept in mind, it will be possible to create more flexible programs with fewer arguments.

Let's introduce another example for application.

      SUBROUTINE SUM2(X, imax, IX, JX, XSUM)

      REAL X(imax,JX)



      XSUM = 0. 

      DO 100 I=1,IX 

      DO 100 J=1,JY 

        XSUM = XSUM + X(I,J) 

  100 CONTINUE 

      END 

This subroutine calculates the sum of a part of the elements in the 2-D array X(I,J). If it is used in the following manner, 

      REAL X(102,102)

      ........

      CALL SUM2(X(2,2), 102, 100, 100, XSUM)

      ........

we obtain the  partial sum for the central 100*100 portion excluding the rim of a 2-D array with dimensions of 102*102, or the sum of the part corresponding to the diagonal of  (2,2)-(101,101). There is no routine in MATH1 assuming such a use, but this subroutine comes in handy in routines for drawing contours in GRPH2.

Among the arguments that can be delivered to the subroutine, there is the "procedure name." This is different from other arguments in that it is not a "variable," and hence, cannot be changed dynamically. However, this enables the specification of functions for calling  subroutines, such as vrfna/vifna in MATH1. In this case, the procedure name specified for actual arguments must be declared as procedure names using EXTERNAL or INTRINSIC statements in the program for calling subroutines.