next up previous contents
Next: Modules Up: Procedures and Array Arguments Previous: Automatic Arrays

Array-valued Functions

As well as returning `conventional' scalar results, functions can return pointers, arrays or derived types. For array valued functions the size of the result can be determined in a fashion which is similar to the way that automatic arrays are declared.

Consider the following example of an array valued function:

    PROGRAM proggie
     IMPLICIT NONE
     INTERFACE ! mandatory
      FUNCTION funnie(ima,scal)
       INTEGER, INTENT(IN) :: ima(:,:)
       INTEGER, INTENT(IN) :: scal
       INTEGER, DIMENSION(SIZE(ima,1),SIZE(ima,2)) :: funnie
      END FUNCTION funnie
     END INTERFACE
     INTEGER, PARAMETER      :: m = 6
     INTEGER, DIMENSION(M,M) :: im1, im2
      ...
     IM2 = funnie(IM1,1) ! invoke
      ...
    END PROGRAM proggie

    FUNCTION funnie(ima,scal)
     IMPLICIT NONE
     INTEGER, INTENT(IN) :: ima(:,:)
     INTEGER, INTENT(IN) :: scal
     INTEGER :: funnie(SIZE(ima,1),SIZE(ima,2))
     funnie(:,:) = ima(:,:)*scal
    END FUNCTION funnie

here the bounds of funnie are inherited from the actual argument and used to determine the size of the result array; a fixed sized result could have been returned by declaring the result to be an explicit-shape array but this approach is less flexible.

Note: by default, functions are assumed to return a scalar result so if the result is a pointer, array or derived type then an explicit interface must be provided.

Now try this question gif

Now try this question gif


next up previous contents
Next: Modules Up: Procedures and Array Arguments Previous: Automatic Arrays

Adam Marshall ©University of Liverpool, 1996
Fri Dec 6 15:03:35 GMT 1996
Not for commercial use.