owend11
Wit Wizard
LEVEL 1
400 XP
In Visual Basic, there are many cases that we use “subs”. Sub procedure or a subroutine that does not return a value. It is commonly used piece of code which you will want to execute in various places in your program. Using this subroutine, you can minimize your code and find it easy to debug if something went wrong with your program. And this subroutine also can be called in any parts of the program.
Sub Procedure Syntax
[Modifiers] Sub SubName [(ParameterList)]
[Statement]
End Sub
• Modifiers – used to access level of the procedure; example: Public, Private, protected, Friend, Protected Friend and information regarding overloading, Overriding, Sharing and Shadowing.
• SubName – it used to indicate the name of a subroutine.
• ParameterList – the specified list of parameters.
Example:
This example will do the addition of the two values inside the sub and the output will be displayed when the sub is called from different sub. And here’s the following code:
Output:
Passing Parameter By values
And these another example will do the adding the two values using the parameter.
Output:
Book traversal links for Sub Procedures
Sub Procedure Syntax
[Modifiers] Sub SubName [(ParameterList)]
[Statement]
End Sub
• Modifiers – used to access level of the procedure; example: Public, Private, protected, Friend, Protected Friend and information regarding overloading, Overriding, Sharing and Shadowing.
• SubName – it used to indicate the name of a subroutine.
• ParameterList – the specified list of parameters.
Example:
This example will do the addition of the two values inside the sub and the output will be displayed when the sub is called from different sub. And here’s the following code:
- Private
Sub
Button1_Click(
ByVal
sender As
System.
Object
, ByVal
e As
System.
EventArgs
)
Handles
Button1.
Click
- 'call the sub AddtwoValues
- AddtwoValues(
)
- End
Sub
- 'creata a sub
- Sub
AddtwoValues(
)
- 'some declaration
- Dim
num1 As
Integer
=
5
- Dim
num2 As
Integer
=
7
- Dim
total As
Integer
- 'do the computation
- total =
num1 +
num2
- 'display the result
- MsgBox
(
total)
- End
Sub
Output:

Passing Parameter By values
And these another example will do the adding the two values using the parameter.
- Private
Sub
Button1_Click(
ByVal
sender As
System.
Object
, ByVal
e As
System.
EventArgs
)
Handles
Button1.
Click
- 'call the sub AddtwoValues
- 'using the two value parameter
- AddtwoValues(
23
, 45
)
- End
Sub
- 'creata a sub with parameters
- Sub
AddtwoValues(
ByVal
num1 As
Integer
, ByVal
num2 As
Integer
)
- 'some declaration
- Dim
total As
Integer
- 'do the computation
- total =
num1 +
num2
- 'display the result
- MsgBox
(
total)
- End
Sub
Output:

Book traversal links for Sub Procedures
- ‹ Select Case Statements
- Up
- TextBox Control ›