Using Subroutines and Functions in Microsoft VBScript

  • 12/29/2006

Working with Functions

You have already used functions that are predefined in VBScript. These form the crux of most of the flexibility of VBScript. VBScript has more than 100 intrinsic functions, which provide the ability to perform string manipulation (mid, len, instr, instrrev), work with arrays (array, ubound, lbound, split, join), and control the way numbers are handled (int, round, formatnumber). With so much functionality, you might wonder why you need to create your own functions. Although you can write much code that does not require functions, there may be times when functions will make it easier to reuse your code. In addition, functions often make it easier to read and understand what the script is doing. In the VideoAdapterRam_Hard Coded.vbs script, video RAM is retrieved in bytes. To convert the number into something more manageable, we convert it into megabytes by dividing the number by 1,048,576. This causes a readability problem, as seen below.

VideoAdapterRAM_HardCoded.vbs

Option Explicit
'On Error Resume Next
Dim strComputer
Dim wmiNS
Dim wmiQuery
Dim objWMIService
Dim colItems
Dim objItem

strComputer = "."
wmiNS = "\root\cimv2"
wmiQuery = "Select AdapterRAM from win32_videoController"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & wmiNS)
Set colItems = objWMIService.ExecQuery(wmiQuery)

For Each objItem in colItems
  WScript.Echo "AdapterRAM: " & objItem.AdapterRAM/1048576
Next

Add a function to convert to megabytes

  1. Open the \My Documents\Microsoft Press\VBScriptSBS\ch15\VideoAdapter Ram_HardCoded.vbs script in Notepad or the script editor of your choice and save it as YourNameVideoAdapterRam_UseFunction.vbs.

  2. At the bottom of the script, begin the function by using the command function call and name the function convertToMeg. Define a single input to the function as intIn. Close out the function by using the End Function command. This is seen below:

    Function convertToMeg(intIN)
    End Function
  3. Between the Function and the End Function statements, divide intIN by 1,048,576 and assign the results to convertToMeg, as seen below:

    convertToMeg = intIN/1048576
  4. In the WScript.Echo line in the main script, call the convertToMeg function and supply objItem.AdapterRAM as the input parameter to the function. The modified output line is seen below:

    WScript.Echo "AdapterRAM: " & convertToMeg(objItem.AdapterRAM)
  5. Save and run the script. You should see the amount of video RAM reported in megabytes. If not, compare your script with \My Documents\Microsoft Press\VBScriptSBS \ch15\ VideoAdapterRam_UseFunction.vbs.

Comparing intrinsic function and user defined function

  1. Open the \My Documents\Microsoft Press\VBScriptSBS\Templates\Blank Template.vbs script in Notepad or the script editor of your choice and save the script as YourNameFunString.vbs.

  2. Add Option Explicit to the first noncommented line.

  3. Use WScript.Echo to print out a line that calls the VBScript String intrinsic function. Tell the String function to repeat a dash character 15 times. This is seen below:

    WScript.Echo "Intrinsic string function",VbCrLf,string(15,"-")
  4. At the bottom of the script, create a function called funString. Have the function accept two input parameters called intIN and strChar. Close out the function by using End Function. This is seen below:

    Function funString(intIN,strChar)
    
    End Function
  5. Inside the funString function, declare a variable to be used as a counter. Call this variable j.

  6. Use a For Each...Next loop to build up the string of repeated characters. Use j to count from one to intIN.

    For j = 1 To intIN
    
    Next
  7. Between For j = 1 to intIN and Next, add funString to itself and to strChar, as seen below:

    funString = funString & strChar
  8. Copy the previous WScript.Echo line and replace the String function with the funString function, as seen below:

    WScript.Echo "User string function",VbCrLf,funString(15,"-")
  9. Save and run your script. The results from the two functions should be exactly the same. If they are not, compare your script with \My Documents\Microsoft Press\VBScriptSBS \ch15\FunString.vbs.