# VBScript Functions

VBScript provides several predefined functions, many of which are summarized in this section.

VBScript Functions

VBScript provides several predefined functions, many of which are summarized in this section. We overview variant functions, math functions, functions for interacting with the user, formatting functions and functions for obtaining information about the interpreter.

Figure 24.12 summarizes several functions that allow the programmer to determine which subtype is currently stored in a variant. VBScript provides function IsEmpty to determine if the variant has ever been initialized by the programmer. If IsEmpty returns True the variant has not been initialized by the programmer.

VBScript math functions allow the programmer to perform common mathematical cal-culations. Figure 24.13 summarizes some VBScript math functions. Note that trigono-metric functions such as Cos, Sin, etc. take arguments expressed in radians. To convert from degrees to radians use the formula: radians = degrees × π / 180.

VBScript provides formatting functions for currency values, dates, times, numbers and percentages. Figure 24.14 summarizes these formatting functions.

Although they are not discussed in this chapter, VBScript provides many functions for manipulating dates and times. Manipulations include adding dates, subtracting dates, parsing dates, etc. Consult the VBScript documentation for a list of these functions.

VBScript also provides functions for getting information about the scripting engine (i.e., the VBScript interpreter). These functions are ScriptEngine (which returns

JScript”, “VBScript” or “VBA”), ScriptEngineBuildVersion (which returns the current build version—i.e., the identification number for the current release), ScriptEngineMajorVersion (which returns the major version number for the script engine) and ScriptEngineMinorVersion (which returns the minor release number). For example, the expression

ScriptEngine() & ", " & ScriptEngineBuildVersion() & ", " _

& ScriptEngineMajorVersion() & ", " & _

ScriptEngineMinorVersion()

evaluates to "VBScript, 5207, 5, 5" (where the numbers are the build version, major version and minor version of the script engine at the time of this writing).

VBScript provides two functions, InputBox and MsgBox, for interacting with the user. Function InputBox displays a dialog in which the user can input data. For example, the statement

intValue = InputBox( "Enter an integer", "Input Box", , _ 1000, 1000 )

displays an input dialog (Fig. 24.15) containing the prompt ("Enter an integer") and the caption ("Input Box") at position (1000, 1000) on the screen. VBScript coordinates are measured in units of twips (1440 twips equal 1 inch). Position (1000, 1000) is relative to the upper-left corner of the screen, which is position (0, 0). On the screen, x coordinates increase from left to right and y coordinates increase from top to bottom.

VBScript functions often take optional arguments (i.e., arguments that programmers can pass if they wish or that can be omitted). Notice, in the preceding call to InputBox, the consecutive commas (between "Input Box" and 1000)—these indicate that an optional argument is being omitted. In this particular case, the optional argument corre-sponds to the initial value displayed in the input dialog—a feature we do not wish to use in this particular call to InputBox. Before using a VBScript function, check the VBScript documentation

msdn.microsoft.com/scripting/default.htm?/scripting/vbscript

to determine whether the function allows for optional arguments.

The underscore character, _, is VBScript’s line-continuation character. A statement cannot extend beyond the current line without using this character. A statement may use as many line-continuation characters as necessary.

When called, function MsgBox displays a message dialog (a sample is shown in Fig. 24.15). For example, the statement

Call MsgBox( "VBScript is fun!", , "Results" )

displays a message dialog containing "VBScript is fun!" with "Results" in the title bar. Although not used here, the optional argument allows the programmer to custom-ize the MsgBox’s buttons (e.g., OK, Yes, etc.) and icon (e.g., question mark, exclamation point, etc.)—see the VBScript documentation for more information on these features. The preceding statement could also have been written as

MsgBox "VBScript is fun!", , "Results"

which behaves identically to the version of the statement that explicitly uses Call. In VB-Script, function calls that wrap arguments in parentheses must be preceded with keyword Call—unless the function call is assigning a value to a variable, as in

a = Abs( z )

We prefer the more formal syntax that uses Call and parentheses to clearly indicate a function call.

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

Related Topics