Statistica Visual Basic Syntax Reference - User-defined Functions and Subroutines

Statistica Visual Basic supports user-defined subroutines and functions, allowing the user to build large, structured programs. Libraries of subroutines and functions can be saved in separate files and included in programs via the Include metacommand '$include:"J:\Filename"

Declaration of functions. The general format of function definitions is:

[Public][Private] Function FunctionName ([ByVal | ByRef] Argument) ReturnType

[Variable Declarations]

[Events]

FunctionName = [Expression]

End Function

The components of the function definition are:

Public/private
Specifies the scope of the function; because Statistica Visual Basic programs are encapsulated within single macro files (which alleviates modularity and importing issues) this is optional.
Function name
The name of the function. The name may also determine the function's return type depending on the suffix (see Variable and Function Naming Conventions).
Arguments
List of variables representing arguments that are passed to the function when it is called; multiple variables are separated by commas. Arguments that are to be changed when returning to the calling function have to be passed by reference and declared using the keyword ByRef (see User-Defined Functions and Subroutines (Arguments)).
Return type
The data format of the expression being returned to the calling function.
Variable declarations
Variables are declared at the top of the function; note that these variables can only be referred to within this function. Declaring additional variables for functions variables is optional.
Events
This is simply any event (e.g., data conversion, message boxes being displayed, retrieving data) which takes place within the function.
FunctionName = [Expression]
Expression represents the data being returned to the calling function.
End Function
Indicates the end of the current function.

The following is an example of a function which takes two integers and returns an integer which is the sum of the two numbers:

Function Add _

(FirstNumber As Integer, SecondNumber As Integer) _
 As Integer

Add = FirstNumber + SecondNumber
 End Function

Declaration of subroutines
The general format of subroutine definitions is very similar to that of functions, except that no return value can be specified:

[Public][Private] Sub SubroutineName ([ByVal | ByRef] Argument)

[Variable Declarations]

[Events]

End Sub

The components of the subroutine definition are:

Public/private
Specifies the scope of the subroutine; because Statistica Visual Basic programs are encapsulated within single macro files (which alleviates modularity and importing issues) this is optional.

Subroutine name. The name of the subroutine.

Arguments
List of variables representing arguments that are passed to the subroutine when it is called; multiple variables are separated by commas. Arguments that are to be changed when returning to the calling function have to be passed by reference and declared using the keyword ByRef (see User-Defined Functions and Subroutines (Arguments)).
Variable declarations
Variables are declared at the top of the subroutine; note that these variables can only be referred to within this subroutine. Declaring additional for subroutine variables is optional.
Events
This is simply any events (e.g., data conversion, message boxes being displayed, retrieving data) which take place within the subroutine.

End Sub. Indicates the end of the current subroutine.

The following is an example of a subroutine which takes three integers and adds the first two integers into the third; note that because the third integer is passed by reference, its value is permanently altered:

Sub Add _
 (FirstNumber As Integer, SecondNumber As Integer, _
 ByRef Result As Integer)

Result = FirstNumber + SecondNumber
 End Sub

Functions vs. subroutines.
As illustrated in the two examples shown above, functions will return a value to the calling program, subroutine, or function. In general, function procedures can be used on the right-hand side of expressions in the same way you use any intrinsic function, such as Sqr, Cos, or Chr. Subroutines cannot be used on the right-hand side of expressions. To return values from subroutines, the respective arguments have to be passed to the subroutine by reference, i.e., using the keyword ByRef (see above). Of course, arguments can also be passed to functions by reference.

Functions and subroutines can be exited at anytime using the expressions Exit Function and Exit Sub , respectively. Note, however, that doing so within a function will not return an expression.