A few words of thanks would be greatly appreciated.

VBScript - The functions and procedures

A function refers to a subroutine that allows the execution of a set of instructions by simply calling in the body of the main program. This notion of using subroutine is usually called function (or procedure) in most programming languages. There two types of procedures used for VBScript:
  • Sub procedures - a set of instructions that doesn't return any value.
  • Function procedures -a set of instructions that returns a value as output.

Procedures allows you to execute series of instructions at various part of the program. It simplifies your code and and size of the program. On the other hand, a function that can call itself is called a recursive function.

VBScript contains predefined functions that may apply to one or more specific types of objects, these functions are called methods.

Declaring a Sub procedure

The definition of a procedure called "declaration". The declaration of a Sub procedure is done between the Sub and End Sub insctructions, using the following syntax:
Sub Name_of_procedure(argument1, argument2, ...)
		   list of instructions
End Sub

Note that:
  • The Sub keyword is followed by the name that is given to the procedure
  • The function name follows the same rules as variable names:
  • The name must start with a letter
  • a procedure name can contain letters, numbers and characters _ and & (spaces are not allowed!)
  • the name of the function, as the variables are case sensitive (differentiation between uppercase and lowercase)
  • The arguments are optional, but if there are no arguments, the parentheses must still be present.
  • Do not forget to close the braces
  • The number of open braces (function, loops and other structures) must be equal to the number of closed parentheses!
  • The same applies to parentheses, brackets or quotes!
  • Once this is done, your procedure will not run until it is called somewhere in the script!

Declaring a Function procedure

A Function procedure is similar to a Sub procedure, with the difference that it can return a value.
The declaration of a Function procedure is done between the Function and End Function instructions, using the following syntax:

Function Name_of_procedure(argument1, argument2, ...)
		   List of instructions
End Function

The Function procedure can return a value of the Variant!

The following function converts Euros in Francst:

Function FrancEuro(prix)
 FrancEuro = prix / 6.55957
End Function

Calling a function

To execute a function, just call it by writing its name (case sensitive) followed by an open parenthesis (and possibly arguments) and then a closing parenthesis:


To call a procedure:

Call Procedure_name()

It is also possible to call a procedure as follows:

Procedure_name arguments

Original document published on CommentcaMarche.net

A few words of thanks would be greatly appreciated.

Ask a question
CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jeff Pillou, founder of CCM.net. CCM reaches more than 50 million unique visitors per month and is available in 11 languages.
This document, titled "VBScript - The functions and procedures," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (https://ccm.net/).