| Prev | Next | Start of Chapter | End of Chapter | Contents | Glossary | Index | Comments | (3 out of 15)

User-Defined Functions

You create a user-defined function by using a function definition.

To create a user-defined function:

G2 invokes the Text Editor automatically so that you can enter your function. The syntax is:

where:

function-name
Specifies the name of the function.
argument [,...]
Describes the arguments for the function, and are substituted for the arguments in the function definition.
expression
Defines the computation of the function using a symbolic, arithmetic, or logical expression, and can include other user-defined or system-defined functions.

When you close the text editor, G2 attaches a rectangle representing the new function definition to the mouse. Click on any workspace to place the definition on that workspace. G2 transfers the definition rectangle to the workspace at the point of the click, and displays the text of the function in the rectangle. You can click the definition to edit it, display its table, transfer it to a different workspace, or delete it.

For example, the following text specifies the area function:

On a workspace, the area function definition looks like this:


After creating this function, you can refer to it by name or arguments in procedures or expressions. An example of using the area function is:

You can reference any system-defined or user-defined function in a function definition, but you cannot use functions recursively because they provide no way to terminate the recursion. Use a procedure when recursive computation is needed.

| Prev | Next | Start of Chapter | End of Chapter | Contents | Glossary | Index | Comments | (3 out of 15)

Copyright © 1997 Gensym Corporation, Inc.