VBScript procedures
A procedures is a segment of code grouped into a single entity. The great thing about procedures is that you can re-use them. By doing so, you eliminate the need to re-type the same code over and over again. Once you implement a procedure, you can use its code by calling that procedure.
This lesson focuses on:
- Creating procedures
- Using parameters
- Calling procedures
- Returning values
- VBScript's built-in procedures
Creating procedures
There are two types of procedures in VBScript - sub procedures and Function procedures.
Sub procedures
A Sub procedure can perform some actions, but CANNOT return values.
A Sub procedure is enclosed in Sub and End Sub.
Syntax:
Sub nameOfSubProcedure() actions to perform End Sub
Example:
Sub printText()
document.write("Here is some text");
End Sub
Function procedures
A Function procedure can perform some actions, and can return values.
A Function procedure is enclosed in Function and End Function.
Syntax
Function nameOfFunction() actions to perform End Function
Example:
Function printText()
document.write("Here is some text");
End Function
Using parameters
Parameters are variables placed inside the parentheses of a sub procedure or a Function procedure which are used by the code inside the sub procedure or Function procedure in some way. Parameters are separated by commas.
Using parameters with a Sub procedure
Syntax for using parameters with a Sub Procedure:
Sub nameOfSubProcedure(param1, param2, etc.) actions to perform End Sub
Example:
Sub printName(name)
document.write("Your name is " & name);
End Sub
The above procedure takes a value supplied by its name parameter and prints a message on a webpage.
Using parameters with a Function procedure
Syntax for using parameters with a Function procedure:
Function nameOfSubProcedure(param1, param2, etc.) actions to perform End Function
Example:
Function printName(name)
document.write("Your name is " & name);
End Function
The above Function procedure takes a value supplied by its name parameter and prints a message on a webpage.
Calling procedures
Once you create a procedure, how do you actually use it? You have to call that procedure.
Calling a Sub procedure
To call a Sub procedure, refer to the name of the Sub procedure with the Call statement, and pass the appropriate parameter values to it (if the Sub procedure has any parameters).
Syntax:
Call subProcedureToCall()
Example:
Sub printMessage()
document.write("VBScript is a scripting language.")
End Sub
Call printMessage()
Output:
Calling a Function procedure
To call a Function procedure, refer to the name of the Function procedure with the Call statement, and pass the appropriate parameter values to it (if the Function procedure has any parameters).
Syntax:
Call FunctionProcedureToCall()
Example:
Function printMessage()
document.write("VBScript is a scripting language.")
End Sub
Call printMessage()
Output:
Returning values
Function procedures can be used to return values. By doing so, the Function procedure simply acts as a value after its code has been executed. This way, a variable can take the value of a Function procedure.
Syntax for returning a value from a Function procedure:
Function aFunction() aFunction = value End Function
Example:
Function greet()
greet = "user"
End Function
document.write("Hello "&greet())
Output:
VBScript's built-in procedures
VBScript has several built-in procedures you can use for various purposes.
Some of VBScript's built-in procedures:
-
abs()
This procedure will return the absolute value of a number.
Example:
<script type="text/vbscript"> document.write(abs(-5)) </script>
Output:
5 -
Len()
This procedure will return the length of a text string.
Example:
<script type="text/vbscript"> document.write(Len("Hello")) </script>Output:
5 -
IsNumeric
This procedure will return a boolean (true/false) value indicating whether a supplied value is a number or not.
Example:
<script type="text/vbscript"> Dim a a = "five" document.write(IsNumeric(a)) </script>
Output:
False -
Date()
This procedure will return the current system date.
Example:
<script type="text/vbscript"> document.write("Today's date is: "&Date()) </script>Output:
Today's date is: 2/1/2007




