Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP Fortran 90 Programmer's Reference: HP Series 700/800 Computers > Chapter 4 Arrays

Array functions

» 

Technical documentation

» Feedback
Content starts here

 » Table of Contents

 » Glossary

 » Index

Functions may be used in array expressions. As well as returning a scalar result, a function may also be defined to return an array result. Array functions may be used in any array expression provided that they do not appear:

  • In an input list

  • On the left side of an assignment statement (unless returning the result from within a function)

Array functions may also be used in an array expression wherever a scalar function reference is allowed but must be conformable —that is, the function result must have the same shape as the expression. Functions that return arrays are also known as array-valued functions and may be either:

  • Intrinsic functions

  • User-defined functions

Intrinsic functions

Intrinsic functions are an integral part of the Fortran 90 Standard and are described in detail in Chapter 11. The group of functions known as elemental procedures and transformation procedures have particular relevance to array expressions. Elemental procedures are specified for scalar arguments, but when used with an array argument will return an array result with the same shape as its argument(s); each element of the result is as if the function were applied to each corresponding element of the argument. Examples of elemental intrinsic procedures are the mathematical functions SQRT and SIN.

A transformational procedure on the other hand generally has one or more array arguments which the procedure operates on as a whole, and usually returns an array result whose elements may depend not only on the corresponding elements of the arguments but also on the values of other elements of the arguments. The RESHAPE intrinsic mentioned earlier in the chapter is an example of a transformational procedure; other examples are the intrinsic functions SUM and MATMUL.

User-defined functions

User-defined functions are not elemental in that they are defined to return either a scalar result or to return an array result; also they cannot be used interchangeably with scalar or array arguments. A scalar function may of course appear in an array expression but the effect, as with any other scalar, is to first broadcast its value throughout a conformable array. A reference to a user-defined array function must obey the rules for functions in general, and must also conform to the shape of the expression in which it appears.

User-defined functions are described in Chapter 7.

The following example shows how an array-valued function may be referenced.

PROGRAM main
! the following interface block describes the
! characteristics of a function genrand; the
! function inputs a single integer scalar and
! returns a real array of rank-one with an
! extent equal to the value of its argument
INTERFACE
    FUNCTION genrand(n)
    INTEGER:: n
    REAL, DIMENSION (n)::genrand
    END FUNCTION genrand
END INTERFACE
REAL :: a(100)
REAL :: b(10,10)
a = genrand(SIZE(a))
! the array a is set to the result returned by
! the function genrand, note that the left
! and right hand side are conformable.
b = RESHAPE(a  + genrand(100),(/ 10, 10 /))
! each element of a is added with the
! corresponding element of the result returned
! by genrand to form an intermediate rank-one
! result that is passed into the intrinsic
! function RESHAPE.  In this example, the 
! RESHAPE intrinsic transforms its argument 
! into a 10 by 10 array; again the left and 
! right hand side are conformable.
...
END PROGRAM main
Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1996 Hewlett-Packard Development Company, L.P.