Variables and Values of Variables

Contractual, local, global, and static variables. Variables that can be used in subroutine and function procedures fall into four categories: Arguments that are passed to the procedure, local variables, global variables, and static variables.

Contractual variables. Contractual variables (or passed variables) are variables that are passed between functions and subroutines. The original variable will be local to the calling function, but it can be used in other functions that it is passed to. The degree of access that the receiving function has to the variable's value is determined by whether it was passed by value or by reference [see STATISTICA Visual Basic Syntax Reference - User-Defined Functions and Subroutines (Arguments)]. Refer to STATISTICA Visual Basic Syntax Reference - User-Defined Functions and Subroutines for further details about how to pass variables as arguments between procedures.

Local variables. All variables that are declared within a function or subroutine are local. Local means that the values of those variables are only accessible within the respective procedure, and no naming conflicts will arise if the same variable name is used in another procedure or the main program.

Declaring local variables
To create a variable that is specific to a function or subroutine, simply declare it within the body of the procedure (preferably at the top) as such:

Sub Main

Dim ReportName As String

End Main

Unless this variable is passed (either by value or by reference), no other procedure within your program may either view or manipulate it.

Note: once a function or subroutine ends, its local variables are deleted. Each time the procedure is called, it will create new instances of its local variables. If you wish for a function to retain its variables, then those variables must be declared as static.

Global variables. All variables or arrays that are defined in the Declaration section of a macro are global. Global means that the values of those variables can be accessed and changed inside any procedure, even though they were not passed as arguments.

Declaring global variables
To make other variables and arrays globally visible to the procedures that follow, declare them in the Declaration section of your macro using the Dim keyword. For example:

'Global integer holding the number of
 'open reports in the application
 Dim gNumberOfReports As Integer
 Sub Main
 End Sub

Any procedure within your program may access and manipulate this variable in the same manner that it accesses its own local variables.

Static variables. Static variables behave the same way as local variables (in that no other procedure may have access to them without passing them as arguments), except that they persist even after the function or subroutine has ended. The following demonstrates a function which internally keeps track of how many times it is called by utilizing a static variable:

Sub main
 'Call the counting function five times
 For i = 1 To 5

Count

Next i
 End Sub

 Sub Count
 'Instead of using Dim to declare the variable,
 'use the keyword Static
 Static HowManyTimesCalled As Integer
 'Each time this function is called this
 'variable will be incremented.
 'Because it's static, this variable is
 '"remembered" everytime the function is used
 HowManyTimesCalled = HowManyTimesCalled + 1
 MsgBox Str(HowManyTimesCalled)
 End Sub