• We just launched and are currently in beta. Join us as we build and grow the community.

Sub Procedures

owend11

Wit Wizard
O Rep
0
0
0
Rep
0
O Vouches
0
0
0
Vouches
0
Posts
73
Likes
85
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
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:

  1. Private

    Sub

    Button1_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    Button1.

    Click
  2. 'call the sub AddtwoValues
  3. AddtwoValues(

    )
  4. End

    Sub

  5. 'creata a sub
  6. Sub

    AddtwoValues(

    )
  7. 'some declaration
  8. Dim

    num1 As

    Integer

    =

    5
  9. Dim

    num2 As

    Integer

    =

    7
  10. Dim

    total As

    Integer
  11. 'do the computation
  12. total =

    num1 +

    num2
  13. 'display the result
  14. MsgBox

    (

    total)
  15. End

    Sub

Output:
sub1.png


Passing Parameter By values
And these another example will do the adding the two values using the parameter.
  1. Private

    Sub

    Button1_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    Button1.

    Click
  2. 'call the sub AddtwoValues
  3. 'using the two value parameter
  4. AddtwoValues(

    23

    , 45

    )
  5. End

    Sub

  6. 'creata a sub with parameters
  7. Sub

    AddtwoValues(

    ByVal

    num1 As

    Integer

    , ByVal

    num2 As

    Integer

    )
  8. 'some declaration
  9. Dim

    total As

    Integer
  10. 'do the computation
  11. total =

    num1 +

    num2
  12. 'display the result
  13. MsgBox

    (

    total)
  14. End

    Sub

Output:
sub2.png


Book traversal links for Sub Procedures

  • ‹ Select Case Statements
  • Up
  • TextBox Control ›

 

440,010

316,559

316,568

Top