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

Visual Basic (VB.NET) Procedures and Functions

bibil

Package Manager Expert
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
137
Likes
66
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In creating a modular program using Visual Basic.Net we always used Procedures and Functions. A procedure and function is a piece of code in larger program. They perform a specific task. Reducing duplication of code, Decomposing complex problems into simpler pieces, Improving of code, Reuse of code, and Information hiding are the common advantage of using procedures and functions.

Procedures

A procedure does not return any value and the statements is enclosed inside the Sub and End Sub.

Example:
  1. Public

    Class

    testsub

  2. Private

    Sub

    Button3_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    Button3.

    Click

  3. Dim

    email As

    String
  4. 'Get the value from textbox named txteamil
  5. 'and stored to string variable email
  6. email =

    Txtemail.

    Text
  7. 'call the sub procedure named checkemel
  8. Call

    checkemel(

    email)
  9. End

    Sub

  10. 'this is our Procedure
  11. Sub

    checkemel(

    ByVal

    email As

    String

    )
  12. 'test if the user dont e something on the textbox
  13. If

    email =

    ""

    Then
  14. 'if true then it will ask the user to write the email address
  15. MsgBox

    (

    "Please Provide correct email address!"

    )
  16. Else
  17. 'else it simply display the value inputed by the user
  18. MsgBox

    (

    "Your email address is!"

    &

    email)
  19. End

    If
  20. End

    Sub

  21. End

    Class

Output
sub.png

Functions

A function does something returns a value and using Visual Basic the statements inside Function, End Function.

Example:
  1. Public

    Class

    testFunctions

  2. Private

    Sub

    Button6_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    Button6.

    Click
  3. Dim

    first As

    Integer
  4. Dim

    second As

    Integer
  5. Dim

    result As

    Integer

  6. 'assign values to specified variables
  7. first =

    Val

    (

    num1.

    Text

    )
  8. second =

    Val

    (

    num2.

    Text

    )

  9. 'calls the function to perform addition
  10. result =

    AddTwoNumbers(

    first, second)



  11. If

    result =

    0

    Then
  12. MsgBox

    (

    "Please try again "

    )
  13. Else
  14. MsgBox

    (

    "The answer is "

    &

    result)
  15. End

    If

  16. End

    Sub

  17. Function

    AddTwoNumbers(

    ByVal

    firsts As

    Integer

    , ByVal

    seconds As

    Integer

    )

    As

    Integer

  18. Dim

    answer As

    Integer

  19. answer =

    firsts +

    seconds

  20. 'AddTwoNumbers = answer
  21. Return

    answer

  22. End

    Function
  23. End

    Class

Output
functions.png


 

452,292

323,526

323,535

Top