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 tutorial focuses on:
- Creating procedures
- Using parameters
- Calling procedures
- Returning values
- VBScript's built-in procedures
There are two types of procedures in VBScript - Sub procedures and Function procedures.
A Sub procedure can perform some actions, but CANNOT return values.
A Sub procedure is enclosed in Sub and End Sub.
A Function procedure can perform some actions, and can return values.
A Function procedure is enclosed in Function and End Function.
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
The above procedure takes a value supplied by its name parameter and prints a message on a webpage.
Using parameters with a Function procedure
The above Function procedure takes a value supplied by its name parameter and prints a message on a webpage.
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 there are any).
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 there are any).
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.
VBScript's built-in procedures
VBScript has several built-in procedures you can use for various purposes.
- abs() - will return the absolute value of a number
- Len() - will return the length of a text string
- IsNumeric - will return a boolean (true/false) value indicating whether a supplied value is a number or not
- Date() - will return the current system date