Next: Range Checking
Up: Data Type Conversions the DCV Routines
Previous: Introduction

## An Example of Data Type Conversion

As an example, a generic subroutine for finding the mean of the data contained in an array, where the array is of varying type but the mean is always REAL, might look like the following routine.

```          SUBROUTINE MEAN<T>( N, ARRAY, MEAN )
*+
*  Name:
*     MEAN<T>
*  Purpose:
*     Calculate the mean value contained in a <COMM> array.
*  Invocation :
*     CALL MEAN<T>( N, ARRAY, MEAN )
*  Description :
*     This calculates the mean value of then data contained in the given
*     <COMM> array, and returns the value in the REAL variable MEAN.
*  Arguments:
*     N = INTEGER( Given )
*        The dimension of the array.
*     ARRAY( N ) = <TYPE>( Given )
*        The array containing the data to be averaged.
*     MEAN = REAL( Returned )
*        The mean of the values contained in the array.
*                   "
*     (the rest of the standard prologue)
*                   "
*  Type Definitions:
IMPLICIT NONE
*  Arguments Given:
INTEGER N
<TYPE> ARRAY( N )
*  Arguments Returned:
REAL MEAN
*  External References:
INCLUDE 'DCV_EXT'          ! Data conversion external functions
*  Local Variables:
INTEGER I                  ! Array index
<LTYPE> SUM                ! Sum of the data in the array
*  Internal References:
INCLUDE 'DCV_FUN'          ! Data conversion statement functions
*-

*  Initialise the sum to zero.
SUM = 0<CONST>

*  Accumulate the sum of the contents of the array.
DO I = 1, N
SUM = SUM + ARRAY( N )
END DO

*  Divide by the dimension of the array to obtain the mean, using
*  DCV_<T>TOR to convert from <COMM> into REAL.
MEAN = DCV_<T>TOR( SUM ) / REAL( N )

END
```

In the above example, for the INTEGER version of the routine for instance, the DCV_ITOR( SUM ) statement will be converted to REAL( SUM ) by the statement function in DCV_FUN.

Next: Range Checking
Up: Data Type Conversions the DCV Routines
Previous: Introduction

GENERIC --- A Utility for Preprocessing Generic Fortran and C Subroutines