HP 3000 Manuals

Functions [ HP FORTRAN 77/iX Programmer's Guide ] MPE/iX 5.0 Documentation


HP FORTRAN 77/iX Programmer's Guide

Functions 

FORTRAN functions can be grouped into categories, as summarized in Table
5-2 .

          Table 5-2.  Categories of FORTRAN Functions 

---------------------------------------------------------------------
|                          |                                        |
|     Type of Function     |              Description               |
|                          |                                        |
---------------------------------------------------------------------
|                          |                                        |
| Function Subprogram      | A user-defined function                |
|                          |                                        |
---------------------------------------------------------------------
|                          |                                        |
| Statement Function       | A user-defined single statement        |
|                          | function                               |
|                          |                                        |
---------------------------------------------------------------------
|                          |                                        |
| Intrinsic Function       | A FORTRAN built-in function            |
|                          |                                        |
---------------------------------------------------------------------

A function name in an expression causes the function to be evaluated; the
function then assigns a value to the function name.  As with a
subroutine, a function can also return values through its arguments or
through common blocks.  However, these side effects should be avoided
because they inhibit clarity.  The effect of a function should be the
calculation of a single result returned through the function name.

Function Subprograms 

A function subprogram is a user-written FORTRAN function in a program.  A
function is invoked by using the function name, followed by the argument
list.

The FUNCTION statement is the first statement of a function.  Here are
some examples of FUNCTION statements:

     FUNCTION time()

     INTEGER*4 FUNCTION add(k, j)

     LOGICAL FUNCTION key_search(char_string, key)

The function names are:  time, add, and key_search.  Values are passed to
function subprograms by arguments (k,j, char_string, and key in the
statements above are arguments) or common blocks.  An argument list is
not required, but you must use parentheses to differentiate the function
name from a simple variable.

An example of a user-defined function is shown below:

           PROGRAM main

           READ (5,'(2F10.0)') a, b

           x = bigger(a, b)

           WRITE (6,100) a, b, x
     100   FORMAT (1X, 2F10.2, /, 1X, 'The largest value is', F10.2)
           END

           FUNCTION bigger(a, b)    ! Function to return the larger value
           IF (a .LT. b) THEN
               bigger = b
           ELSE
               bigger = a
           ENDIF

           END

A function can contain declaration, assignment, input/output, and flow
control statements; a function cannot contain another FUNCTION statement,
a BLOCK DATA, a SUBROUTINE, or a PROGRAM statement.  As an extension to
the ANSI 77 standard, an HP FORTRAN 77 function subprogram can be
recursive.  That is, a function can contain a direct or indirect
reference to itself.

The END statement transfers control back to the calling program where the
function call was made.  The function subprogram always returns to the
expression from which it was invoked.  Alternate returns are not allowed
in function subprograms.

The RETURN statement also transfers control back to the calling program.
You only need to use RETURN statements for returning to the calling
program from a place other than the end of a function.  The last line of
a function must be an END statement.

To associate a value with the function subprogram name, the function name
must be used within the function in one or more of these ways:

   *   Specified on the left side of an assignment statement

   *   Included as an element of an input list in a READ statement

   *   Be an actual argument of a function or subroutine reference

Some examples demonstrating how to associate a value to the function name
are shown below.  Consider this function:

           INTEGER FUNCTION factorial(n)
           INTEGER fact, n
           fact = 1

           DO 10 i = 2,n
              fact = fact * I
      10   CONTINUE

           factorial = fact

           END

In the function factorial above, the value fact is assigned to the
function name factorial.  Note that the DO loop will not be executed if n
equals zero or one.

Here is another function:

     FUNCTION tot(num,sum)
     REAL num, sum

     IF (num .GE. 0.0) THEN
         tot = sum + num
     ELSE
         READ (5,*) tot
     ENDIF

     END

In the function tot above, a value is assigned to the function name tot
in one of two ways:  by appearing on the left side of an assignment
statement or by appearing in the input list of a READ statement.

Finally, look at the function next1:

     FUNCTION next1(back)

     IF (back .GT. 1.5) THEN
         CALL gtfwrd(next1)
     ELSE
         CALL gtback(next1)
     ENDIF

     END

The function next1 shows how a function name is associated with a value
in one of two subroutines.  Within the subroutines, next1 must be
assigned a value.

Because a value is assigned to the function subprogram name, the value's
data type must be defined.  The data type associated with the function
name is determined in one of these ways:

   *   If the data type is included with the FUNCTION statement, the name
       is assigned that type.  For example, a FUNCTION statement
       explicitly specified as an integer looks like this:

            INTEGER FUNCTION funcname()

       A function name cannot have the data type specified more than once
       in a program.  For example, using the following statements
       together is illegal:

            INTEGER FUNCTION funcname()

            INTEGER funcname

   *   If the data type is not included in the FUNCTION statement, the
       function name can be declared in a type statement within the
       function.  The type statements are:  CHARACTER, COMPLEX*8,
       COMPLEX*16, INTEGER*2, INTEGER*4, LOGICAL*1, LOGICAL*2, LOGICAL*4,
       REAL*4, REAL*8, and REAL*16, as well as BYTE, COMPLEX, DOUBLE
       COMPLEX, DOUBLE PRECISION, INTEGER, LOGICAL, and REAL.

       For example, the following statements define an integer function:

            FUNCTION funcname()

            INTEGER funcname

   *   If the data type is not included in the FUNCTION statement and is
       not declared in a type statement, the type is assigned implicitly
       according to the first letter of the function name.  Unless
       modified by an IMPLICIT statement, function names beginning with
       the letters A through H and O through Z define a REAL data type;
       letters I through N define an INTEGER data type.

The data type of the value associated with the function name in each
program unit must agree with the type of the function.

When you reference a character function, the length of the function must
be the same as that declared in the function.  There is always agreement
of length if a length of asterisk (*) is specified in the function.  For
example, a character function and a program that calls the function are
shown below.  The function returns the character string with all
preceding and trailing blanks removed.

           PROGRAM test

           CHARACTER*20 input_string, result, stringtrim

           DO WHILE (input_string(1:1) .NE. '0')
              WRITE (6,'(A,NN)') 'Enter a string: '
              READ (5,'(A)') input_string
              result = stringtrim(input_string,length)
              WRITE (6,'(A1,A,A1)') ':', result(1:length), ':'
           END DO
           END

           CHARACTER*(*) FUNCTION stringtrim(string,length)
           CHARACTER*(*) string
           INTEGER length, left, right, i, j
           DO k= 1, LEN(stringtrim)
              stringtrim(k:k)=" "    ! Initialize stringtrim
           END DO

           left = 1

           right = LEN(string)    ! The intrinsic function LEN returns
     C                              the length of the string.

           DO WHILE ((string(left:left) .EQ. ' ') .AND. (left .LT. right))
             left = left + 1
           END DO

           DO WHILE ((string(right:right) .EQ. ' ') .AND. (right .GT. left))
             right = right - 1
           END DO

           length = right - left + 1
             DO 10 i = 1, length
             stringtrim(i:i) = string(left:left)
             left = left + 1
      10   CONTINUE

     C  The default is to return one blank if a string is all blanks

           END

A sample run, where Delta represents a blank, looks like this:

     Enter a string: string
     :string:
     Enter a string:  Delta  Delta  Delta  Delta four
     :four:
     Enter a string: three Delta  Delta  Delta
     :three:
     Enter a string:  Delta  Delta  Delta blanks Delta  Delta  Delta   Delta
     :blanks:
     Enter a string:  Delta  Delta  Delta  Delta  Delta  Delta  Delta
     : :
     Enter a string: 0
     :0:

The value returned by the function is the value last assigned to the
function name at the time a RETURN statement is executed in the function.

Consider this example of a calling program unit and a function
subprogram.  The program asks for input of two numbers m and n and
computes the combinations of m items taken n at a time.  That is, the
function computes the following:

                     m! 
                  ---------
                  n!(m-n)! 

The function subprogram factorial is invoked in the expression that
calculates result.

           PROGRAM main
           INTEGER*4 factorial, result

           WRITE (6,*) 'Enter m and n: '
           READ (5,*) m, n
           result = factorial(m) / (factorial(n) * factorial(m-n))
           WRITE (6,'(1X, I5, " things taken ", I5,
          c                       " at a time = ", I8)') m, n, result
           END

           INTEGER FUNCTION factorial(num)
           INTEGER fact, num

           fact = 1
           DO 10 i=2, num
              fact = fact * i
      10   CONTINUE

           factorial = fact

           END

Two runs might look like this:

     Enter m and n: 7, 4
         7 things taken    4 at a time =    35

     Enter m and n: 10, 2
        10 things taken    2 at a time =    45

Statement Functions 

A statement function is a user-defined single-statement computation that
can only be called in the program unit that defines it.  The form of a
statement function is similar to an arithmetic, logical, or character
assignment statement.  Only one value is returned from a statement
function.

A statement function is invoked just like a function subprogram.  When
your program calls a statement function, the dummy arguments are replaced
by actual arguments within the function expression.  For example, if you
define the function calculate as:

     calculate(x, y, z) = y * x * (y + x) - z

the statement:

     result = a + calculate(a, b, c)

gives the same result as if you had written:

     result = a + (b * a * (b + a) - c)

Following are some more examples of statements functions:

     root(a, b, c) = (-b + SQRT(b * b - 4. * a * c)) / (2. * a)

     disp(c, r, h) = c * 3.1416 * r * r * h

     indexq(a,j) = IFIX(a) + j - ic

The statement function is called with its symbolic name and an actual
argument list in an arithmetic, logical, or character expression.  For
example, the program below defines and calls a statement function.

     PROGRAM functionex

     root(a, b, c) = (-b + SQRT(b * b - 4. * a * c)) / (2. * a)
     var1 = 2.0
     var2 = -9.0
     var3 = 4.0
     var4 = root(var1, var2, var3)

     END

The value of var4 will be 4.0.

A statement function can call another statement function.  For example,
the program below defines a statement function that calls another
statement function.

     PROGRAM functionex2

     add(a, b, c) = a + b + c
     add25(d, e, f) = add(d, e, f) + 25.0
     DATA value1, value2, value3 /5.0, 10.0, 15.0/
     result = add25(value1, value2, value3)
     PRINT *, result

     END

All statement function definitions must precede the first executable
statement in the program unit and must follow any specification
statements in a program unit.  The name of a statement function cannot be
the same as a variable name, an array name, or a record name in the same
program unit.

All arguments in the dummy argument list are simple variables and assume
the value of the actual arguments in the same program unit when the
function is invoked; that is, dummy arguments are replaced by actual
arguments.  The actual arguments can be variables, constants, and
expressions.  Variables in the statement function not included in the
argument list assume the current value of the variable name in the
program unit.  For example, in the statement function:

     indexq(a, j) = IFIX(a) + j - ic

the variable ic is not an argument, but is an ordinary variable defined
outside the statement function.

A call to a statement function does not cause control to "jump" to
another section of code; instead, the compiler substitutes the statement
function code into the program code.  A statement function cannot call
itself directly or indirectly; that is, statement functions are not
recursive.

The data type of a statement function is determined in the same way as
for a variable.  The type is either declared explicitly in a type
statement or determined implicitly by the function name.  If the type of
the statement function is not the same type as the expression to the
right of the equal sign in the statement function and if the function
name and expression are both numeric, both logical, or both character,
the expression is converted to the type of the function.  For example,
the statement function:

     f(i) = i + j

has integer variable i and j and a real function name f.  The expression
i + j is converted to real.

An intrinsic function is a built-in function that is available to your
program.  Intrinsic functions perform operations such as converting a
value from one data type to another and perform basic mathematical
functions, such as finding sines, cosines, and square roots of numbers.
The HP FORTRAN 77/iX Reference Manual describes each intrinsic function
in detail and discusses the data types of arguments allowed and the
argument and function type.



MPE/iX 5.0 Documentation