define function
Full Description
swirl
Syntax
  define result-type? function func-name
         arg-list? ((as func-body) | elsewhere)

  or

  define external result-type? function
         func-name arg-list? as
         external-name (in library-name)?


Purpose

A function, like a variable, must be defined before it can be used. The define function definition allows OmniMark to know how to interpret the arguments passed to the function, and to know what kind of result it is getting back from the function.

This is a simple example of a function definition:

  define counter function count-words-in value stream s as
     local counter word-count
     set word-count to 0
     repeat scan s
     match white-space * [any-text except white-space]+
        increment word-count
     again
     return word-count

The keyword external is used to indicate an external function. For example, the following code illustrates the method for defining an external function:

  define external function get-db-record (value stream key,
                                          value stream value) as
         "gdbrec" in function-library "mylib.dll"

The result type of a function is generally a switch, a counter, a stream, or an opaque type. It can also be used to indicate when an external function is either an external output function or an external source function. When the result type is given, it always precedes the function name.

The argument list in a function definition describes the class and type of each argument to a function, if any. It has the following form:

((argument-template (argument-separator argument-template)*)?)

The outer parentheses are optional, but have an effect on the syntax:

  • In a parenthesized argument list, the argument separator can be a comma or an unquoted OmniMark name.
  • In an unparenthesized argument list, the argument separator must be an unquoted OmniMark name.

The maximum number of arguments for an idividual function call is 16383.

The argument template describes how the caller passes each argument to the function. Each argument passed must match its corresponding argument template. The syntax is:

argument-class shelf-type argument-name (optional (initial {default-value})?)?

The argument class is value, read-only, modifiable or remainder.

The shelf type is counter, switch, or stream.

The following is an example of a function definition that uses a first argument separator to clarify the meanings of the arguments in the calling syntax for this function:

  define counter function calculate-cylinder-volume
     radius value counter radius
     height value counter height
  as
     return 314 * radius * radius * height / 100

This is what the function call might look like:

  set volume to calculate-cylinder-volume radius r height h

Copyright © OmniMark Technologies Corporation, 1988-1998.