|Top Previous Next|
At the global declaration level script source-code (unit) can contain Sub and Function declarations as well as global statements, including variable declaration Dim statements:
Sub P(A, B)
Dim i, j
Dim k = 3
MsgBox(A + B + F)
Dim X, Y
Dim Z = 7
MsgBox("This is a global statement")
Global variable declaration if NativeVB language is a statement (Dim statement) and can be used anywhere in the code. However, the scope of local variables is the whole Sub or Function. The scope of Global Variables - all script code. Thus, duplicate variable names are not allowed inside the corresponding scope:
Dim x = 7
If x > 7 Then
Dim y = 9
Return x + y
Global statements can reside anywhere in the source code unit, it can be written even between Sub/Function declarations. Global statements are executed at the script control activation is the top-to-bottom order.NativeVB language allows to specify variable initializers exactly in Dim statement for both: global and local variables. Initializers works just like an ordinal assignments and executes each time, when the execution point reaches the Dim statement; for example the initializer of the Dim statement placed inside a loop will be executed at each loop iteration.
The order of subs/functions/variables declarations is not meaningful, procedures/functions can refer to each other, unrelated to the declaration order. Since NativeVB is untyped language, no type specification is allowed in variables/parameters/functions declaration.
Sub/function parameters are declared as a list of parameter names delimited with the comma "," symbol. By default, parameters have a reference semantic. ByVal or ByRef keywords can be used to specify explicitly, whether the argument should be passed with value or reference semantic. Parameter list can be empty. Here the examples of parameter declarations:
Sub P(A, B, ByVal C)
Sub P(A, ByRef B, ByRef C)
To return value from the function, simply assign the value to the pseudo-variable with the name, equal to the function name. You can also use modern, Vb .Net like Return statement:
F = 7
Script code can use Exit Sub or Exit Function statements to immediately return from the parent Sub or Function. It is a syntax error to use these statements outside of any Sub or Function (e.g. in global code).