Author
Sanskruti ( Ambitious contributor )
Yet to provide details about himself
Recent Articles
- Handling the large numeric values while exporting to excel., Started by sameer_havakajoka in Visual Basic [VB]
- Printing Text Directly To Default Printer, Started by naimish in Visual Basic [VB]
- Visual Baisc RPG attack combat system script Tutorial, Started by XXxxImmortalxxXX in Visual Basic [VB]
- VB6 Small Backup Program, Started by Darkness1337 in Visual Basic [VB]
- Fading Effect In VB.Net, Started by pradeep in Visual Basic [VB]
Similar Articles
- Working with operators in VBScript, Started by Sanskruti in HTML/DHTML - JavaScript/VBScript
Introduction
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
Sub Subroutine_Name(argument1, argument2, ..., argumentn)
...code within the subroutine
End Sub
The following are some valid subroutine names:
- WelcomeTheUser
- PrintInvoice
- Meters2Yards
- User.Welcome
- 2Printer
- Miles*1.609
Code: VB
Sub ShowMessage(CurrentMessage)
MsgBox CurrentMessage, vbOkOnly, "Important Message"
End Sub
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
Sub ShowAboutMessage
MsgBox "This Web page was designed by the WebWizard."
End Sub
Code: VB
Sub ShowAboutMessage(Message)
MsgBox Message
End Sub
Calling a Subroutine
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
ShowMessage "This is the message."
Code: VB
Call ShowMessage("This is the message.")
Code: VB
ShowAboutMessage
or
Call ShowAboutMessage()
or you could use
Call ShowAboutMessage
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
Exit Sub
Code: VB
Sub ConvertFeetToInches
Feet = InputBox("How many feet are there?")
If Feet < 0 Then
Exit Sub
Else
MsgBox "This equals " & Feet * 12 & " inches."
End If
End Sub
Code: VB
Sub ConvertFeetToInches
Feet = InputBox "How many feet are there?")
If Feet >= 0 Then
MsgBox "This equals " & Feet * 12 & " inches."
End If
End Sub













Linear Mode

