04.01.2015 Views

CR1000 Manual - Campbell Scientific

CR1000 Manual - Campbell Scientific

CR1000 Manual - Campbell Scientific

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.

Section 7. Installation<br />

7.8.4 Subroutines<br />

A subroutine is a group of programming instructions that is called by, but runs<br />

outside of, the main program. Subroutines are used for the following reasons:<br />

• To reduce program length. Subroutine code can be executed multiple times in<br />

a program scan.<br />

• To ease integration of proven code segments into new programs.<br />

• To compartmentalize programs to improve organization.<br />

By executing the Call() instruction, the main program can call a subroutines from<br />

anywhere in the program.<br />

A subroutine has access to all global variables (p. 455). Variables local to a<br />

subroutine (local variables (p. 457) ) are declared within the subroutine instruction.<br />

Local variables can be aliased (as of 4-13; OS 26) but are not displayed in the<br />

Public table. Global and local variables can share the same name and not<br />

conflict. If global variables are passed to local variables of different type, the<br />

same type conversion rules apply as apply to conversions among variables<br />

declared as Public or Dim. See Expressions with Numeric Data Types (p. 143) for<br />

conversion types.<br />

Note To avoid programming conflicts, pass information into local variables and /<br />

or define global variables to be used exclusively by a subroutine.<br />

CRBasic example Subroutine with Global and Local Variables (p. 187) shows the<br />

use of global and local variables within a simple subroutine. Variables counter()<br />

and pi_product are global. Variable i_sub is global but used exclusively by<br />

subroutine process. Variables j() and OutVar are local since they are declared as<br />

parameters in the Sub() instruction,<br />

Sub process(j(4) AS Long,OutVar).<br />

Variable j() is a four-element array and variable OutVar is a single-element<br />

array. The call statement,<br />

Call ProcessSub (counter(1),pi_product)<br />

passes five values into the subroutine: pi_product and four elements of array<br />

counter(). Array counter() is used to pass values into, and extract values from,<br />

the subroutine. The variable pi_product is used to extract a value from the<br />

subroutine.<br />

Call() passes the values of all listed variables into the subroutine. Values are<br />

passed back to the main scan at the end of the subroutine.<br />

CRBasic Example 37.<br />

Subroutine with Global and Local Variables<br />

'Global variables are those declared anywhere in the program as Public or Dim.<br />

'Local variables are those declared in the Sub() instruction.<br />

'Program Purpose: Demonstrates use of global and local variables with subroutines<br />

'Program Function: Passes 2 variables to subroutine. Subroutine increments each<br />

'variable once per second, multiplies each by pi, then passes results back to<br />

'the main program for storage in a data table.<br />

187

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

Saved successfully!

Ooh no, something went wrong!