![]() |
Subroutines in VBscript
On 1st May, 2007
|
AuthorSanskruti ( Ambitious contributor )
Recent Articles
Similar Articles
A subroutine 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. Maybe you need to accomplish that task in various places throughout your code. All you need to do is 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. A subroutine is a block of code that can be called from anywhere in a program to accomplish a specific task. Subroutines can accept starting data through subroutine declaration variables called parameters. However, subroutines do not automatically return a result code or an argument to the caller. You declare subroutines using the Sub keyword and end them using the End Sub statement. The structure of a subroutine is Code: VB
The following are some valid subroutine names:
Code: VB
Often, a subroutine might not require any arguments, and you can drop the parentheses. Suppose you have a subroutine that simply displays information to the user. In that case, the subroutine doesn't need any arguments, as in the following case: Code: VB
Code: VB
Now that you've learned how to create a subroutine, how do you call one? You can call a subroutine throughout the rest of the application once you've declared and created it. You can call subroutines by using the Call keyword or just entering the name of the subroutine on a line of code. For example, to call a subroutine called ShowMessage, you could enter Code: VB
Code: VB
Code: VB
Exiting a Subroutine The code within your subroutine will execute until one of two things happens. First, the subroutine might get down to the last line, the End Sub line, which terminates the subroutine and passes the baton back to the caller. This statement can appear only once at the end of the subroutine declaration. The second possibility is that VBScript could execute the following code statement: Code: VB
Code: VB
Code: VB
|
|
|
#2 |
|
Go4Expert Member
Join Date: Jul 2008
Posts: 17
Thanks: 0
Thanked 1 Time in 1 Post
Rep Power: 0 ![]() |
Re: Subroutines in VBscript
I have this ASP code <% sub mysub mytestvariable = 111 end sub call mysub response.write mytestvariable %> Why does the above code show BLANK when I response.write mytestvariable Shouldn't it output 111? |
|
|
|
|
|
#3 | |
|
Newbie Member
|
Re: Subroutines in VBscriptQuote:
Code: VBscript
If you wanted a call to the subroutine to write out the variable without changing the scope of the variable you could also include the Response.Write inside the subroutine: Code: VBscript
Another limitation is that subroutines do not return anything to the main body of code. If you wanted to assign a value to a private variable inside the subroutine but write the variable value in the main body of code, you would have to use a function instead of a subroutine and assign the function the value of the variable: Code: VBscript
|
|
|
|
|
![]() |
|
| Currently Active Users Reading This Article: 1 (0 members and 1 guests) | |
| Article Tools | Search this Article |
| Display Modes | |
| Bookmarks | |
|
|
|
||||||||||||||||||||||||||||||||