XL Fortran for AIX 8.1

Language Reference

FUNCTION

Purpose

The FUNCTION statement is the first statement of a function subprogram.

Format



   .------------.
   V            |
>>---+--------+-+--FUNCTION--name--+------------------+--------->
     '-prefix-'                    |   (1)       (2)  |
                                   '-*-------len------'
 
>--(--+---------------------+--)--+---------------------------+-><
      '-dummy_argument_list-'     '-RESULT--(--result_name--)-'
 
 


Notes:


  1. IBM Extension.

  2. IBM Extension.


prefix
is one of the following:
type_spec
RECURSIVE
FORTRAN 95 Begins PURE FORTRAN 95 Ends
FORTRAN 95 Begins ELEMENTAL FORTRAN 95 Ends

type_spec
specifies the type and type parameters of the function result. See Type Declaration for details about type_spec.

name
is the name of the function subprogram

+-------------------------------IBM Extension--------------------------------+

len
is an unsigned, positive, integer literal constant that cannot specify a kind type parameter. It represents the permissible length specifications for its associated type. It can be included only when the type is specified. The type cannot be DOUBLE PRECISION, DOUBLE COMPLEX, BYTE, or a derived type.

+----------------------------End of IBM Extension----------------------------+

Rules

At most one of each kind of prefix can be specified.

The type and type parameters of the function result can be specified by either type_spec or by declaring the result variable in the declaration part of the function subprogram, but not by both. If they are not specified at all, the implicit typing rules are in effect. A length specifier cannot be specified by both type_spec and len.

If RESULT is specified, result_name becomes the function result variable. name must not be declared in any specification statement in the subprogram, although it can be referenced. result_name must not be the same as name. If RESULT is not specified, name becomes the function result variable.

If the result variable is an array or pointer, the DIMENSION or POINTER attributes, respectively, must be specified within the function body.

If the function result is a pointer, the shape of the result variable determines the shape of the value returned by the function. If the result variable is a pointer, the function must either associate a target with the pointer or define the association status of the pointer as disassociated.

If the result variable is not a pointer, the function must define its value.

If the name of an external function is of derived type, the derived type must be a sequence derived type if the type is not use-associated or host-associated.

The function result variable must not appear within a variable format expression, nor can it be specified in a COMMON, DATA, integer POINTER, or EQUIVALENCE statement, nor can it have the PARAMETER, INTENT, OPTIONAL, or SAVE attributes. The AUTOMATIC or STATIC attributes can be specified if it is not an array or pointer, or if it is not of character or derived type.

The function result variable is associated with any entry procedure result variables. This is called entry association. The definition of any of these result variables becomes the definition of all the associated variables having that same type, and is the value of the function regardless of the entry point.

If the function subprogram contains entry procedures, the result variables are not required to be of the same type unless the type is of character or derived type, or if the variables have the POINTER attribute, or if they are not scalars. The variable whose name is used to reference the function must be in a defined state when a RETURN or END statement is executed in the subprogram. An associated variable of a different type must not become defined during the execution of the function reference, unless an associated variable of the same type redefines it later during execution of the subprogram.

Recursion

The RECURSIVE keyword must be specified if, directly or indirectly:

A function that directly invokes itself requires that both the RECURSIVE and RESULT keywords be specified. The presence of both keywords makes the procedure interface explicit within the subprogram.

If name is of type character, its length cannot be an asterisk if the function is recursive.

+-------------------------------IBM Extension--------------------------------+

If RECURSIVE is specified, the result variable has a default storage class of automatic.

You can also call external procedures recursively when you specify the -qrecur compiler option, although XL Fortran disregards this option if the FUNCTION statement specifies either RECURSIVE or RESULT.

+----------------------------End of IBM Extension----------------------------+

+---------------------------------Fortran 95---------------------------------+

Elemental Procedures

For elemental procedures, the keyword ELEMENTAL must be specified. If the ELEMENTAL keyword is specified, the RECURSIVE keyword cannot be specified.

+-----------------------------End of Fortran 95------------------------------+

Examples

RECURSIVE FUNCTION FACTORIAL (N) RESULT (RES)
  INTEGER RES
  IF (N.EQ.0) THEN
    RES=1
  ELSE
    RES=N*FACTORIAL(N-1)
  END IF
END FUNCTION FACTORIAL
PROGRAM P
  INTERFACE OPERATOR (.PERMUTATION.)
    ELEMENTAL FUNCTION MYPERMUTATION(ARR1,ARR2)
      INTEGER :: MYPERMUTATION
      INTEGER, INTENT(IN) :: ARR1,ARR2
    END FUNCTION MYPERMUTATION
  END INTERFACE
 
  INTEGER PERMVEC(100,150),N(100,150),K(100,150)
  ...
  PERMVEC = N .PERMUTATION. K
  ...
END

Related Information


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]