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.

User-defined Code<br />

• <strong>Caché</strong> saves the current value of the system variable $TEST when it enters the function and restores it when the function<br />

terminates. Any change in the $TEST value during execution of the function will be discarded when the function exits,<br />

unless you include code to explicitly save it by some other means.<br />

8.3.2.6 Location of Functions<br />

You can define a user-defined function within the routine that references it, or in a separate routine where multiple programs<br />

can reference it. Recommended practice is to use one routine to contain all your user-defined function definitions. In this<br />

way, you can easily locate any function definition to examine or update it.<br />

8.3.2.7 Invoking a User-defined Function<br />

You can invoke a user-defined function using either the $$ prefix, or by using the Do command. The $$ form provides the<br />

most functionality, and is generally the preferred form.<br />

<strong>Using</strong> the $$ Prefix<br />

You can invoke a user-defined function in any context in which an expression is allowed. A user-defined function call takes<br />

the form:<br />

$$name([param [,...]])<br />

where<br />

• name specifies the name of the function. Depending on where the function is defined, name can be specified as:<br />

– label — A line label within the current routine.<br />

– label^routine — A line label within the named routine that resides on disk.<br />

– ^routine — A routine that resides on disk. The routine must contain only the code for the function to be performed.<br />

A routine is defined within a namespace. You can use an extended routine reference to execute a user-defined function<br />

that is located in a routine defined in a namespace other than the current namespace:<br />

WRITE $$myfunc^|"SAMPLES"|routine<br />

• param specifies the values to be passed to the function. The supplied parameters are known as the actual parameter<br />

list. They must match the formal parameter list defined for the function. For example, the function code may expect<br />

two parameters, with the first being a numeric value and the second being a string literal. If you specify the string literal<br />

for the first parameter and the numeric value for the second, the function may yield an incorrect value or possibly<br />

generate an error. Parameters in the formal parameter list always have New invoked by the function. See the New<br />

command. Parameters can be passed by value or by reference. See Parameter Passing. If you pass fewer parameters<br />

to the function than are listed in the function’s formal parameter list, parameter defaults are used (if defined); if there<br />

are no defaults, these parameters remain undefined.<br />

<strong>Using</strong> the Do Command<br />

You can invoke a user-defined function using the Do command. (You cannot invoke a system-supplied function using the<br />

Do command.) A function invoked using Do does not return a value. That is, the function must generate a return value, but<br />

the Do command ignores this return value. This greatly limits the use of Do for invoking user-defined functions.<br />

To invoke a user-defined function using Do, you issue a command in the following syntax:<br />

Do label(param[ ,...])<br />

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

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

Saved successfully!

Ooh no, something went wrong!