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 Compiler for HP-UX: HP Fortran Programmer's Reference > Chapter 4 Arrays

Array-valued functions

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Glossary

 » Index

A function may be array-valued; that is, its return value may evaluate to an array of values rather than to a scalar. Array-valued functions may appear in any array expression except:

  • In an input list

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

Array-valued 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.

The following sections describe intrinsic functions and user-defined functions that are array-valued.

Intrinsic functions

Elemental procedures and transformation procedures have particular relevance to array expressions. Elemental procedures—for example, SQRT and SIN—are specified for scalar arguments, but with an array argument they return an array-valued result with the same shape as the argument. Each element of the result is as if the function were applied to each corresponding element of the argument.

A transformational procedure—for example, RESHAPE, SUM, and MATMUL—generally has one or more array arguments that the procedure operates on as a whole, and usually returns an array-valued 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.

User-defined functions

User-defined functions can return either a scalar-valued result or an array-valued result. A scalar function can appear in an array expression; its effect is to broadcast its value throughout a conformable array. A reference to a user-defined array-valued 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 “External procedures”.

The following code segment illustrates two array-valued functions, genrand (user-defined) and RESHAPE (intrinsic):

PROGRAM main

! The following interface block describes the characteristics of
! the 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)

! set array a to the result returned by the function genrand;
! note that the left and right hand side are conformable
a = genrand(SIZE(a))

! add each element of a to the corresponding element of the
! result returned by genrand, forming an intermediate rank-one
! result that is passed into the intrinsic function RESHAPE.
! This intrinsic transforms its argument into a 10 by 10 array.
! Again, the left and right hand side are conformable.
b = RESHAPE(a + genrand(100),(/ 10, 10 /))
.
.
.
END PROGRAM main
Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© Hewlett-Packard Development Company, L.P.