contentsconceptssyntaxlibrariessampleserrorsindex
Full text search
declaration/definition   define function    
  Related Syntax   Related Concepts  

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:

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

    Related Syntax
   elsewhere
   external-function
   optional
   set function-library of external-function
 
Related Concepts
   Basic structure of a rule-based program
   External functions
   External functions: declaring
   External functions: identifying
   Function bodies
   Function libraries: declaring a default
   Function names
   Function predefinitions
   Function types and anatomy
   Functions
   Functions and declaration-free programs
   Functions: applying "save" to arguments
   Functions: argument classes
   Functions: argument lists
   Functions: argument recognition
   Functions: argument separator ambiguity
   Functions: argument separators
   Functions: argument shelf types
   Functions: greedy argument separator recognition
   Functions: passing a shelf as a modifiable argument
   Functions: passing a shelf as a read-only argument
   Functions: passing expressions as arguments
   Functions: passing multiple values as a single argument
   Functions: recursive
   Functions: resolving ambiguous argument separators
   Functions: result types
   Functions: shelves and arguments
   Functions: side effects
   Functions: side effects in rule headers
   Functions: tail recursion
   Functions: the differences between value and read-only arguments
   Functions: uses
   Functions: using the same argument separator in different definitions
 
----

Top [CONTENTS] [CONCEPTS] [SYNTAX] [LIBRARIES] [SAMPLES] [ERRORS] [INDEX]

Generated: April 21, 1999 at 2:01:03 pm
If you have any comments about this section of the documentation, send email to [email protected]

Copyright © OmniMark Technologies Corporation, 1988-1999.