VBScript is a handy language to do some daily jobs in Windows. The VBScript language origins from BASIC, which can be dated back in the MSDOS era. The VBScript is installed by default ever since Windows XP and the script files (*.VBS) can be interpreted by the wscript.exe or cscript.exe in the windows system folder. The Windows Scripting Host (WSH) environment makes VBScript powerful because it grants the VBScript the ability to invoke the COM (Component Object Model). The syntax of VBScript is easy to understand and the following will discuss the three usages of parentheses in VBScript.
Array Index (To indicate an indexer on an array)
The parentheses in VBScript can be used to declare the array or access the array using subindex. For example,
1 | Dim Arr(3) |
Dim Arr(3)
declares an array of 4 sizes (not 3, the valid index is from 0 to 3). And we can use UBound(Arr) (in this case, returns 3), to obtain the upper bound of an array. To access the element in the array, use e.g. Arr(0) to Arr(3).
Part of Expression (As an operator in an expression)
This is similar to most of other programming languages. The parentheses can be used to change the priority of an arithmetic expressions. For example, (1+2)*3 will result in 9 instead of 7.
Used to enclose an argument list when defining or calling procedure/function
In VBScript, the keyword Sub is used to declare a procedure that does not return a value and the keyword Function is used to declare a procedure that returns a value.
1 2 3 4 5 6 7 8 9 10 11 12 13 | Sub Test Msgbox "Procedure" End Sub Function Test2 Test2 = "Function" End Function ' Calling Procedure Test ' Calling Function Msgbox Test2 |
Sub Test Msgbox "Procedure" End Sub Function Test2 Test2 = "Function" End Function ' Calling Procedure Test ' Calling Function Msgbox Test2
We can use parentheses to enclose the list of arguments (separated by comma), for example,
1 2 3 4 5 6 7 8 9 10 11 12 13 | Sub Test(a, b) Msgbox a + b End Sub Function Test2(a) Test2 = a End Function ' Calling Procedure Test 1, 2 ' Calling Function Msgbox Test2(3) |
Sub Test(a, b) Msgbox a + b End Sub Function Test2(a) Test2 = a End Function ' Calling Procedure Test 1, 2 ' Calling Function Msgbox Test2(3)
When calling a Sub in VBScript, there are two possibilities.
1 2 | Test 1, 2
Call Test(1,2) |
Test 1, 2 Call Test(1,2)
If you use Test(1,2) the interpreter will complain:
Microsoft VBScript compilation error: Cannot use parentheses when calling a Sub
When calling a Function in VBScript, the parentheses is usually required.
1 2 3 4 5 6 7 8 | Function Test2(a) Test2 = a End Function B = Test2(3) B = Test2 3 ' Error Test2 3 ' works as a procedure Call Test2(3) ' works as a procedure |
Function Test2(a) Test2 = a End Function B = Test2(3) B = Test2 3 ' Error Test2 3 ' works as a procedure Call Test2(3) ' works as a procedure
So we can call functions as procedures, in which case you don’t need the return value. The Call keyword can optionally be used for a subroutine call, or for a function call without an assignment.
The VBScript has ByRef and ByVal keywords to specify that the parameters are passed either by value or by reference. If passed by value, the changes inside the function/procedure will not be kept when outside the function/procedure body. If passed by references, the changes will be reflected instead. By default, all parameters are passed by references unless specified ByVal.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | Sub Test1(x) ' Pass by reference x = "Test1" End Sub Sub Test2(ByVal x) x = "Test2" End Sub Sub Test3(ByRef x) x = "Test3" End Sub x = "000" Test1 x Msgbox x ' Test1 Test2 x Msgbox x ' Test1 Test3 x Msgbox x ' Test3 |
Sub Test1(x) ' Pass by reference x = "Test1" End Sub Sub Test2(ByVal x) x = "Test2" End Sub Sub Test3(ByRef x) x = "Test3" End Sub x = "000" Test1 x Msgbox x ' Test1 Test2 x Msgbox x ' Test1 Test3 x Msgbox x ' Test3
If the number of arguments is 1 (only 1 parameter), the following will not throw errors as above.
1 2 3 | x=1 Test1(x) Test3(x) |
x=1 Test1(x) Test3(x)
However, the value “x” will not be changed (acted as ByVal). So what happens here? The parentheses make it as an expression, so the interpreter will think (x) as the expression, and it will evaluate (x) as 1 and pass this value (copy) to the functions. Therefore, any changes inside function will be disregarded.
Here is another example to help understand the problem.
1 2 3 4 5 6 7 8 9 10 11 | y=1 Sub Test1(ByRef x, ByRef y) x=2 y=2 End Sub ' x is passed by value, y is passed by reference Test1 (x), y Call Test1((x), y) MsgBox x & "," & y ' prints 1,2 |
y=1 Sub Test1(ByRef x, ByRef y) x=2 y=2 End Sub ' x is passed by value, y is passed by reference Test1 (x), y Call Test1((x), y) MsgBox x & "," & y ' prints 1,2
In above example, (x) forces it to pass by value even the procedure declares it as ByRef.
To avoid confusion, when you create a function or sub and you can call them with these way:
For no return value:
1 | myFunction "This is a reference" |
myFunction "This is a reference"
For return value:
1 | myValue = myFunction ("This is a reference") |
myValue = myFunction ("This is a reference")
References
1. MSDN
2. StackOverflow
–EOF (The Ultimate Computing & Technology Blog) —
Last Post: How to Get the CPU Temperature on Raspberry PI using BASH Script?
Next Post: Using a external USB cooler fan to drop temperature of Raspberry PI (Model B) from 51 degrees downto 40 degrees for just ten minutes