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
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
The Function procedure can return a value of the Variant!
The following function converts Euros in Francst:
FrancEuro = prix / 6.55957
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:
It is also possible to call a procedure as follows: