Section 7. Installation
308
CRBasic example
Subroutine with Global and Local Variables
(p. 308)
shows the
use of global and local variables. 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.
Subroutine with Global and Local Variables
'This program example demonstrates 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
'Declare Data Table
DataTable
(pi_results,True,-1)
Sample
(1,counter(),IEEE4)
EndTable
'Declare Subroutine
'Declares j(4) as local array (can only be used in subroutine)
Sub
ProcessSub (j(2) As Long,OutVar(2) As Float)
For
i_sub = 1
To
2
j(i_sub) = j(i_sub) + 1
'Processing to show functionality
OutVar(i_sub) = j(i_sub) * 4 * ATN(1)
'(Tip: 4 * ATN(1) = pi to IEEE4 precision)
Next
i_sub
EndSub
Summary of Contents for CR800 Series
Page 2: ......
Page 4: ......
Page 6: ......
Page 32: ......
Page 34: ......
Page 54: ......
Page 92: ......
Page 310: ......
Page 446: ...Section 8 Operation 446 8 11 2 Data Display FIGURE 100 CR1000KD Displaying Data ...
Page 448: ...Section 8 Operation 448 FIGURE 102 CR1000KD Real Time Custom ...
Page 449: ...Section 8 Operation 449 8 11 2 3 Final Storage Data FIGURE 103 CR1000KD Final Storage Data ...
Page 450: ...Section 8 Operation 450 8 11 3 Run Stop Program FIGURE 104 CR1000KD Run Stop Program ...
Page 452: ...Section 8 Operation 452 FIGURE 106 CR1000KD File Edit ...
Page 456: ......
Page 462: ......
Page 523: ...Section 11 Glossary 523 FIGURE 116 Relationships of Accuracy Precision and Resolution ...
Page 524: ......
Page 526: ......
Page 556: ......
Page 558: ......
Page 560: ......
Page 597: ......