Article
· Oct 24, 2016 2m read

NewBie's Corner Session 22 Parameters Part III

NewBie's Corner Session 22 Parameters Part III

Welcome to NewBie's Corner, a weekly or biweekly post covering basic Caché Material.

Parameters

In Sessions’ 20 and 21 on Parameters we saw how to call one routine from another with parameters as well as calling one Label from another with parameters. In these examples, the data was passed through the parameters from the calling routine or label to the called routine or label.

Now suppose you wanted the called routine or label to pass data back?

There are two ways** of doing this.

1) Returning a Status through a User-Supplied Function

2) Using Passed by Reference

 

Returning a Status through a User-Supplied Function

A System-Supplied Function can be identified by a single “$”. Examples are $Get, $Piece and $List. A User-Supplied Function is identified by two “$$”.It is a Function that the User or programmer writes. A Function is identified as a procedure that returns a value. See the example below:

 

Set Status=$$Label(Parm1,Parm2,Parm3)

 

Label(Parm1,Parm2,Parm3)

. . .

. . .

. . .

Quit 1

 

Here the User Defined Function $$Label is called and a return value is passed back to Status. The Quit <parameter> at the end of the Label procedure is what gives a value to Status. By convention a Status of 1 signifies a success and 0 signifies failure.

You may see a status of

“0 fail because of . . . “.

The zero (in the first column) tells the calling code that the function has failed and the text tells why.

This is handy because the calling code may have something like:

Set Status=$$Label(Parm1,Parm2,Parm3)

If ‘+Status write Status.

 

Using Passed by Reference parameters

There are two ways of passing parameters:

(1) By Value

 (1) By Reference

In the last two sessions we have been passing parameters by Value.

When parameters are passed by Reference, the value of the  parameters may be changed by the called routines.

 

Set Status=$$Label(Parm1,.Parm2,Parm3);the dot before Parm2 indicates passed by Reference

 

Label(Parm1,Parm2,Parm3)

. . .

Set Parm2=”new value”

. . .

Quit 1

To indicate Passed by Reference, a dot or period precedes the parameter.

When the calling code get control back, the value of Parm2 has changed.

One final note, an array can only be passed by reference.

** Note: there are of course more ways of passing data from the called routine to the calling routine. However, the two ways described here are the proper or sanctioned ways.

--Mike Kadow

PaulMikeKadow@gmail.com

If you have a comment, please respond through the InterSystems Developer Community, don't send me private email, unless of course you wish to address me only.

See "Newbie's Corner Index" for an index of all NewBies' Posts

Discussion (2)0
Log in or sign up to continue