VBScript
VBScript introduction
VBScript basics
VBScript variables
VBScript procedures
VBScript popup boxes
VBScript conditions
VBScript loops
VBScript arrays
VBScript strings
VBScript date & time
VBScript summary

Programming

Programming intro
Java

Markup

First webpage guide
HTML
XHTML

Browser scripting

Javascript
AJAX

Server scripting

PHP
ASP

Making money online

Make money online

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

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:

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 the Function procedure has any parameters).

Syntax:

Call FunctionProcedureToCall()

Example:

Function printMessage()

document.write("VBScript is a scripting language.")

End Sub

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.

Some of VBScript's built-in procedures:

Practice

Online code editor
Practical examples
Practical exercises
Step-by-step tutorials

Reference

Terms glossary
Reference material

Rate this site

Rate this site
Visitor comments