· Nov 26, 2021

What is the definition of ROUTINE in COS ?

Hello everyone I'm newb using COS and
I would like to know What is the definition of ROUTINE in COS ?
is there anything related to Global?
is it all information that it's possible to persist on the database?

Product version: Caché 2018.1
Discussion (3)3
Log in or sign up to continue

The documentation might be a little bit long, so I try to make it short. If you write down some kind of logic in COS and save it, then you'll get a routine. The easiest  way to create a routine is this, using a terminal session: ZR<ret><tab>WRITE $H<ret>ZS<space>MYROUTINE. (<this are the corresponding control characters>)Doing so, you'll get an INT file called MYROUTINE.INT. You can see it using %RD in a terminal session in the namespace where you saved the routine. You can run it using the command DO ^MYROUTINE or GO ^MYROUTINE

A routine may contain some labels (some name ended with a TAB) which you can use as functions or procedures. A routine is comparable with a class but you can't instanciate a routine like you can do with a class and it does not contain methods or classmethods.

Global is an array stored on disk. It is a nice datastructure to save your data. A routine can write, store or delete some data stored in Global.

Hope this helps to make the things clear to you

When asking about a 'ROUTINE' you may be asking about the difference between a 'Routine', 'Procedure', 'Subroutine', 'Function', 'Label', 'Class' and 'Method'

A 'Routine' is a source code file with name like 'myroutine.mac'.  Source code can also be a 'Method' which is found in a 'Class' file with a name like 'myclass.cls"

A Routine file can contain Procedures, Subroutines, Functions and Labels.  See for some InterSystems documentation.

You can call a subroutine with the statement 'DO MySubroutine^myroutine(arg)'; You can call a function with the expression '$$MyFunction^myroutine(arg)';  You can call the procedure 'MyProcedure^myroutine(arg)' using either the syntax for a subroutine call or function call depending on whether you need the value returned by the procedure; and, You can Goto the source code following a label with the statement 'GO MyLabel^myroutine'.  If you reference a subroutine/function/procedure/label inside a Routine file (e.g. myroutine.mac) and that subroutine/function/procedure/label access (e.g.$$MyFunction^myroutine(arg)) is referencing a name defined in the same Routine file then you do not have to specify the ^Routine name in the call syntax (e.g. $$MyFunction(arg)).

The local variables used inside a subroutine or function are 'public' variables and those named variables are shared with all other subroutines and functions.  You can use the NEW command inside a subroutine or function to temporarily create new public variables without modifying previous instances of public variables with the same names.

The local variables used inside a procedure are private variables.  The private variables are only available inside the procedure.  Those names do not conflict with variables used by any caller of the procedure.  Those names are not available in any code called by the procedure although it is possible to pass either the private variable or the value of the private variable as an argument when calling out of a procedure.  The declaration of a procedure can optionally include a list of public variables.  Names in public list reference the public variable when they are accessed by code in the procedure.  You can use the NEW command with an argument that is a public variable within a procedure.  A label defined inside a procedure is also private and it cannot be referenced by any GO command outside that procedure.

Methods defined in a class file default to being a procedure with private variables and private labels.  However, it is possible to specify that a method is a subroutine or function.   Also, a method procedure declaration can optionally include a list of global variables.