Procedures in VBScript. Subroutines.


Procedures are the logical parts into which a program is divided. The code inside a procedure is run when the procedure is called. VBScript provides different subroutines, functions, and operators that can be used to develop your code. A subroutine (sub procedure) is a container that holds a series of VBScript statements. Suppose you'd like to create a block of code that accomplishes some specific task. May be you need to accomplish that task in various places throughout your code. All you need to do is to create, or declare, the subroutine in your script. Once you've declared the subroutine, you can call it anywhere within your code. When your program calls a subroutine, the flow of the code is temporarily diverted to the statements within the subroutine. Once the subroutine has finished executing, control returns to the code that called the subroutine and execution picks up from there.

Defining subroutines

A subroutine is easy to define.  All you do is follow this syntax:

<SCRIPT>
......
Sub identifier([parameter list])
*** VBScript Code Here ***
End Sub 
......
</SCRIPT>

All lines of code between the Sub and the End Sub will be executed whenever the subroutine is called.  The parameter list is optional. If you need not it, you simply write as follows.

Sub identifier()
*** VBScript Code Here ***
End Sub

A good style of programming is to to name a subroutine as descriptive as you can. The subroutine identifier should adequately describe what the subroutine is for in plain English rather than in some cryptic abbreviations only you understand and might even forget a week later. You must name subroutines using letters and numbers;  the first character should not be a number, and you cannot use symbols. If you enter an invalid name for a subroutine, the VBScript run-time interpreter will alert you of the problem when you attempt to try your script. The following are some valid subroutine names:

MyProc
ShowNumber
HideData Sub1

The following are some unacceptable subroutine names:

2Data
Speed*6.0

Calling Subroutines

To call a Sub procedure without arguments, you can just type the name of the procedure. You might also use the Call statement. In this case, you must add empty parentheses after subroutine identifier.

The following example shows two calls to the MyProc procedure. One uses the Call statement in the code; the other doesn't. Both do exactly the same thing.

 Call MyProc()
MyProc 

Subroutine arguments

An argument or a parameter is a variable that a procedure requires in order to execute. Any time you need preexisting data to perform the task within the subroutine, arguments are very helpful.  The following subroutine accepts an argument

The copyright of the article Procedures in VBScript. Subroutines. in VB Script is owned by Maxim Karetnikov. Permission to republish Procedures in VBScript. Subroutines. in print or online must be granted by the author in writing.

Go To Page: 1 2

Articles in this Topic    Discussions in this Topic