Campbell Scientific CR6 CR6 Measurement and Control System - Page 290
CRBasic Example 62., Subroutine with Global and Local Variables
View all Campbell Scientific CR6 manuals
Add to My Manuals
Save this manual to your list of manuals |
Page 290 highlights
Section 7. Installation A subroutine has access to all global variables (p. 497). Variables local to a subroutine (local variables (p. 500) ) are declared within the subroutine instruction. Local variables can be aliased (as of 4/2013; OS 26) but are not displayed in the Public table. Global and local variables can share the same name and not conflict. If global variables are passed to local variables of different type, the same type conversion rules apply as apply to conversions among variables declared as Public or Dim. See Expressions with Numeric Data Types (p. 162) for conversion types. Note To avoid programming conflicts, pass information into local variables and / or define global variables to be used exclusively by a subroutine. CRBasic example Subroutine with Global and Local Variables (p. 290) shows the use of global and local variables within a simple subroutine. Variables counter() and pi_product are global. Variable i_sub is global but used exclusively by subroutine process. Variables j() and OutVar are local since they are declared as parameters in the Sub() instruction, Sub process(j(4) AS Long,OutVar). Variable j() is a four-element array and variable OutVar is a single-element array. The call statement, Call ProcessSub (counter(1),pi_product) passes five values into the subroutine: pi_product and four elements of array counter(). Array counter() is used to pass values into, and extract values from, the subroutine. The variable pi_product is used to extract a value from the subroutine. Call() passes the values of all listed variables into the subroutine. Values are passed back to the main scan at the end of the subroutine. CRBasic Example 62. Subroutine with Global and Local Variables ' the use of global and local variables with subroutines. ' 'Global variables are those declared anywhere in the program as Public or Dim. 'Local variables are those declared in the Sub() instruction. 'Program Function: Passes two variables to a subroutine. The subroutine increments each 'variable once per second, multiplies each by pi, then passes results back to the main 'program for storage in a data table. 'Global variables (Used only outside subroutine by choice) 'Declare Counter in the Main Scan. Public counter(2) As Long 'Declare Product of PI * counter(2). Public pi_product(2) As Float 'Global variable (Used only in subroutine by choice) 'For / Next incrementor used in the subroutine. Public i_sub As Long 290