VBScript
  1. VBScript intro
  2. VBScript basics
  3. VBScript variables
  4. VBScript procedures
  5. VBScript popup boxes
  6. VBScript conditions
  7. VBScript loops
  8. VBScript arrays
  9. VBScript strings
  10. VBScript date & time
  11. VBScript for IE 11
  12. VBScript summary

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 tutorial focuses on:

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:
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:
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 there are any).

Syntax:
Call subProcedureToCall()
Example:
Sub printMessage() document.write("VBScript is a scripting language.") End Sub Call printMessage()
Output:
VBScript is a scripting language.

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).

Syntax:
Call FunctionProcedureToCall()
Example:
Function printMessage() document.write("VBScript is a scripting language.") End Function Call printMessage()
Output:
VBScript is a scripting language.

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:
Hello user

VBScript's built-in procedures

VBScript has several built-in procedures you can use for various purposes.

VBScript's built-in procedures example:
<script type="text/vbscript"> Dim a a = "five" document.write(abs(-5)) document.write("<br />" & Len("Hello")) document.write("<br />" & IsNumeric(a)) document.write("<br />" & "Today's date is: "&Date()) </script>
Output:
5 5 False Today's date is: 1/1/2010
© Copyright 2013-2014 Landofcode.com
Terms of use | Privacy policy | Copyright information