next up previous contents
Next: Function Example Up: Program Units Previous: External Subroutine Example

 

Function Syntax

Syntax of a (non-recursive) function:

 
 [ tex2html_wrap_inline21746  prefix  tex2html_wrap_inline21748 ] FUNCTION  tex2html_wrap_inline21746  procname  tex2html_wrap_inline21748 ( [ tex2html_wrap_inline21746  dummy args  tex2html_wrap_inline21748 ])

tex2html_wrap_inline21746  declaration of dummy args tex2html_wrap_inline21748

tex2html_wrap_inline21746  declaration of local objects tex2html_wrap_inline21748

...

tex2html_wrap_inline21746  executable stmts, assignment of result tex2html_wrap_inline21748

[ CONTAINS

tex2html_wrap_inline21746  internal procedure definitions tex2html_wrap_inline21748 ]

END [ FUNCTION [ tex2html_wrap_inline21746  procname tex2html_wrap_inline21748 ] ]

here tex2html_wrap_inline21746  prefix tex2html_wrap_inline21748 , specifies the result type. or,
 
 FUNCTION  tex2html_wrap_inline21746  procname  tex2html_wrap_inline21748 ( [ tex2html_wrap_inline21746  dummy args  tex2html_wrap_inline21748 ])

tex2html_wrap_inline21746  declaration of dummy args tex2html_wrap_inline21748

tex2html_wrap_inline21746  declaration of procname type tex2html_wrap_inline21748

tex2html_wrap_inline21746  declaration of local objects tex2html_wrap_inline21748

...

tex2html_wrap_inline21746  executable stmts, assignment of result tex2html_wrap_inline21748

[ CONTAINS

tex2html_wrap_inline21746  internal procedure definitions tex2html_wrap_inline21748 ]

END [ FUNCTION [ tex2html_wrap_inline21746  procname tex2html_wrap_inline21748 ] ]

Functions are very similar to subroutines except that a function should have a type specifier which defines the type of the result. (The type of the function result can either be given as a prefix to the function name or alternatively be given in the declarations area of the code by preceding the function name (with no arguments or parentheses) by a type specifier. It is a matter of personal taste which method is adopted.) For example,

    INTEGER FUNCTION largest(i,j,k)
     IMPLICIT NONE

is equivalent to,

    FUNCTION largest(i,j,k)
     IMPLICIT NONE 
     INTEGER largest

The function name, tex2html_wrap_inline21746  procname tex2html_wrap_inline21748 , is the result variable so a function must contain a statement which assigns a value to this name; a routine without one is an error.

The type of an external function must be given in the calling program unit. It is good practise to attribute this declaration with the EXTERNAL attribute. A valid declaration in the calling program unit might be,

    INTEGER, EXTERNAL :: largest

Functions can return results of any type, kind and rank (including user defined types and pointers) and, as with subroutines, RECURSIVE functions must be explicitly declared (see Section gif).

Note that due to the possibility of confusion between an array reference and a function reference the parentheses are not optional.




next up previous contents
Next: Function Example Up: Program Units Previous: External Subroutine Example

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