25.01.2015 Views

Using Caché ObjectScript - InterSystems Documentation

Using Caché ObjectScript - InterSystems Documentation

Using Caché ObjectScript - InterSystems Documentation

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Procedures in Detail<br />

If there are more variables in the formal list than there are parameters in the actual list, and a default value is not provided<br />

for each, the extra variables are left undefined. Your function code should include appropriate If tests to make sure that<br />

each function reference provides usable values.<br />

When passing parameters to a user-defined function, you can use passing by value or passing by reference. You can mix<br />

passing by value and passing by reference within the same function call. See Parameter Passing.<br />

Parameter passing can be done by reference or by value.<br />

• System-supplied functions — these can be passed parameters by value only.<br />

• User-defined functions — these can be passed parameters by value or by reference.<br />

• Subroutines can be passed parameters by value or by reference.<br />

• Procedures can be passed parameters by value or by reference.<br />

8.2.5.1 Passing By Value<br />

To pass by value, specify an actual value, an expression, or an unsubscripted local variable name in the actual parameter<br />

list. In the case of an expression, <strong>Caché</strong> first evaluates the expression and then passes the resulting value. In the case of a<br />

variable name, <strong>Caché</strong> passes the variable’s current value. Note that a specified variable name must already exist and must<br />

have a value.<br />

<strong>Caché</strong> implicitly creates and declares any non-public variables used within a procedure, so that already-existing variables<br />

with the same name in calling code are not overwritten. It places the existing values for these variables (if any) on the<br />

program stack. When it invokes the Quit command, <strong>Caché</strong> executes an implicit Kill command for each of the formal<br />

variables and restores their previous values from the stack.<br />

In the following example, the Set commands use three different forms to pass the same value to the referenced Cube procedure.<br />

Do Start()<br />

Start() PUBLIC {<br />

Set var1=6<br />

Set a=$$Cube(6)<br />

Set b=$$Cube(2*3)<br />

Set c=$$Cube(var1)<br />

Write !,"a: ",a," b: ",b," c: ",c<br />

Quit 1<br />

}<br />

Cube(num) PUBLIC {<br />

Set result = num*num*num<br />

Quit result<br />

}<br />

8.2.5.2 Passing By Reference<br />

To pass by reference, specify a local variable name or the name of an unsubscripted array in the form:<br />

.name<br />

With passing by reference, a specified variable or array name does not have to exist before the function reference. Passing<br />

by reference is the only way you can pass an array name to a function.<br />

The period preceding the variable or array name is required. It distinguishes passing by reference from passing by value<br />

using a variable name. If you specify a numeric literal that starts with a decimal point, the <strong>Caché</strong> <strong>ObjectScript</strong> compiler<br />

correctly interprets it as a value rather than a reference.<br />

In passing by reference, each variable or array name in the actual list is bound to the corresponding variable name in the<br />

function’s formal list. Passing by reference provides an effective mechanism for two-way communication between the<br />

referencing routine and the function. Any change that the function makes to a variable in its formal list is also made to the<br />

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

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

Saved successfully!

Ooh no, something went wrong!