25.01.2015 Views

Using Caché ObjectScript - InterSystems Documentation

Using Caché ObjectScript - InterSystems Documentation

Using Caché ObjectScript - InterSystems Documentation

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

8<br />

User-defined Code<br />

<strong>Caché</strong> comes with many system-supplied functions (sometimes known as “intrinsic” functions), which are described in<br />

the <strong>Caché</strong> <strong>ObjectScript</strong> Language Reference. As in other languages, <strong>ObjectScript</strong> allows you to create named code blocks<br />

that you can invoke directly. Such blocks are known as procedures. Strictly speaking, in <strong>ObjectScript</strong> terminology, a code<br />

block that is a procedure has a specific syntax and structure.<br />

The syntax of a procedure definition is as follows:<br />

ProcedureName(Parameters) [PublicVariables]<br />

{<br />

// code...<br />

Quit ReturnValue<br />

}<br />

The elements of the procedure, here called ProcedureName, are:<br />

• Parameters (zero or more) — These can be of any type and, as is typical of <strong>ObjectScript</strong>, you do not need to declare<br />

their types when you define the procedure. By default, they are passed by value (not by reference). Unless otherwise<br />

specified, their scope is local to the procedure. For more information on parameters generally, see the section “Procedure<br />

Parameters.”<br />

• References to public variables (zero or more) — These, too, can be of any type. The procedure can both reference and<br />

set such a variable’s value. For more information on public variable references, see the section “Procedure Variables.”<br />

• Declaration that the procedure is public (optional) — By default, procedures are private, which means that you can<br />

only call them from elsewhere in the same routine (in <strong>ObjectScript</strong> terminology, a routine is a file containing one or<br />

more procedures or other user-defined code blocks). You can also create procedures that are public, using the PUBLIC<br />

keyword after the procedure name. Public procedures can be called from other routines or methods. For more information<br />

on public and private procedures, see “Public and Private Procedures.”<br />

• Code — The code in a procedure has all the features available in <strong>Caché</strong> <strong>ObjectScript</strong>. Procedure code can also include<br />

Basic and Java. The code is delimited by curly braces and is also known as a procedure block.<br />

• Return value (optional) — This is the value that the procedure returns, and, must be a standard <strong>ObjectScript</strong> expression.<br />

Flow control within a procedure can specify various return values using computed expression values, multiple Quit<br />

statements, or both.<br />

Note:<br />

For those familiar with versions of <strong>Caché</strong> before version 5 (and code written for those versions), procedures represent<br />

an advance from coding that was previously available with subroutines and user-defined functions. Procedure<br />

parameters are automatically local in scope within the procedure. They do not require a New command to ensure<br />

that they do not overwrite other values, since they are private to the procedure and do not interact with the symbol<br />

table. Also, the explicit declaration of public variables allows you to refer to global variables within an application,<br />

such a bank-wide interest rate; it also allows you to create and set values for variables within the procedure that<br />

are available to the rest of an application.<br />

<strong>Using</strong> <strong>Caché</strong> <strong>ObjectScript</strong> 75

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!