Subroutines
The sample script has two custom subroutines, too. These are just like functions, except that they just do something; they don't return any result afterwards.
Sub MapDrive(sLetter, sUNC)
Set oNet = WScript.CreateObject("WScript.Network")
oNet.MapNetworkDrive sLetter, sUNC)
End Sub
Sub MapPrinter(sUNC)
Set oNet = WScript.CreateObject("WScript.Network")
oNet.AddWindowsPrinterConnection sUNC
oNet.SetDefaultPrinter sUNC
End Sub
These subroutines are declared with the word Sub, followed by the name of the subroutine. Like a function, these two subroutines each accept some input parameters. Unlike a function, they never set their name to some value, which is why they don't return a value.
VBScript has intrinsic (built-in) subroutines, only they're called statements. A simple statement, like Beep, simply makes the computer beep.
Subroutines serve the same purpose as a function: Although mapping a drive or a printer obviously isn't difficult (taking only two or three lines of code), there's no reason I should have to type those lines of code over and over. Encapsulating the functionality into a subroutine means I can reuse the code repeatedly in one script, and easily paste it into other scripts, saving myself work.
|