+ Reply to Thread
Results 1 to 2 of 2

Thread: VB.NET from beginner to advanced programmer Part 13 - Subs and Functions

  1. #1
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    VB.NET from beginner to advanced programmer Part 13 - Subs and Functions

    Welcome to the VB.NET tutorial series: "VB.NET from beginner to advanced programmer" which will take you from the very beginning to be a good programmer. VB.NET is a good first language for new programmers so this 21 part long series is written for completely beginners but it will also works perfectly fine if you already know another programming language.


    VB.NET from beginner to advanced programmer
    1. Introduction and Installation
    2. Objects and Events
    3. Variables
    4. The basic data types
    5. Logical Operators
    6. Relational Operators
    7. If statements Then
    8. Arithmetical Operators
    9. Loops Part 1
    10. Arrays
    11. Loops Part 2
    12. Try Catch statements
    13. Subs and Functions
    14. Difference between Scopes
    15. Select Statements
    16. Multidimensional arrays
    17. Structures
    18. Classes
    19. Enumerations
    20. Advanced Comments
    21. Compiling Directives





    One thing I meantioned as early in party two is that there exists something that's called a sub and an Event block is one. A sub is a block of code where most of all codes has to be in(not variable declarations, classes and structures, for example). A sub must then be called in some way, by another sub/function(function is a sort of sub but we comes to that later on) or by an object through an event. This means everything has to begin with an event in one way or another.

    Here below I show a simply example on how to create and use your own sub. one of the subs is called by the event block which is called when a button is clicked:



    Code:
    Private Sub cmdExample_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExample.Click
        ExampleSub()
        ExampleSub()
    End Sub
    
    Private Sub ExampleSub()
        MessageBox.Show("This is inside ExampleSub.")
    End Sub

    When we click the button the program will start running at the beginning of cmdExample_Click which is the event block for cmdExample.click. From here we call ExampleSub by just writing its name and two parentheses("ExampleSub()"). Now the program will continue at the beginning of ExampleSub, here a messagebox will be shown. When the program has reached the end of ExampleSub it goes back where it was before. Since we have wrote "ExampleSub()" twice, the sub is called again. This is what you use subs and functions for, you can call the sub from anywhere instead of writing the same code everywhere.






    Using parameters:


    Parameters are like variables to a sub that you set each time you call the sub. A parameter can be wither ByVal or ByRef but more to that soon. If you want you can also set the parameter to optional, this means what it sounds like, you don't have to set its value. But to the right of an optional parameter you can't have a non-optional parameter. We modify they code we had before to include some parameters. Here's the example on how to use parameters:


    Code:
    Private Sub cmdExample_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExample.Click
        ExampleSub(3, 2, 5)
        ExampleSub(3, 5)
    End Sub
    
    Private Sub ExampleSub(ByVal X As Integer, ByVal Y As Integer, Optional ByVal Z As Integer = 1)
        If Z <> 1 Then
            MessageBox.Show("X*Y*Z = " & X * Y * Z)
        Else
            MessageBox.Show("X*Y = " & X * Y)
        End If
    End Sub
    We have now added 3 Integer parameters to ExampleSub. Z is optional and therefor we have to give it an default value, Z will get this value if we aren't using it when calling the sub. Except where and how to declare them parameters work as variables when they are ByVal. When they are ByRef there will be another difference, but more to that later. Inside ExampleSub we're using an if statement to check if Z has its default value. If it has we show the product of only X and Y but if it has another value we shows the product of X, Y and Z. Then in the our event block for the button we first call ExampleSub(3, 2, 5). This will run ExampleSub with X=3, Y=2 and Z=5. Since Z doesn't have its default value the program will use that too in the calculation and we will see: "X*Y*Z = 30". Then we'll call ExampleSub again, this time with X=3 and Y=5. Since we doesn't set the value of Z, it will keep its default value. Since it has its default value the program will show us: "X*Y = 15"







    ByVal and ByRef:

    When declaring a parameter you can choose to declare it By Value(ByVal) or
    By Reference(ByRef). ByVal will only copy the value and use it without changing the original value. ByRef however will not copy the value but instead get a "link" to the original value and when we change the value of our parameter declared by reference it's actually only linking to the original value so that is what we change. Of course I will show you an example on how this works:


    ByVal:
    Code:
    Private Sub cmdExample_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExample.Click
        Dim nOriginalValue As Integer = 10
    
        ExampleSub(nOriginalValue)
    
        MessageBox.Show(nOriginalValue)
    End Sub
    
    Private Sub ExampleSub(ByVal nParameterValue As Integer)
        MessageBox.Show(nParameterValue)
        nParameterValue = 5
    End Sub


    ByRef:
    Code:
    Private Sub cmdExample_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExample.Click
        Dim nOriginalValue As Integer = 10
    
        ExampleSub(nOriginalValue)
    
        MessageBox.Show(nOriginalValue)
    End Sub
    
    Private Sub ExampleSub(ByRef nParameterValue As Integer)
        MessageBox.Show(nParameterValue)
        nParameterValue = 5
    End Sub

    The only difference in the above codes are which one of ByVal and ByRef the parameter is declared as. In both we first declare an Integer variable with the value 10. Then we call ExampleSub with nParameterValue=nOriginalValue. A messagebox is then showed with the value of the parameter and then we change the parameter's value to 5. Then when ExampleSub has ended we show the value of nOriginalValue in a messagebox.

    In the ByVal example, we'll first see "10". The change will not affect the original value so we'll see "10" again.

    In the ByRef example, we'll first see "10". The change of the parameter will here change the original value so we'll see "5" in the second messagebox.





    Functions:


    By replacing sub with function when we create a sub we gets a function instead. The difference is that a Function may and must return a value. To do this we use the functions name as a variable to give it values. We can also use "Return <value>" where value is the value we want the function to return. When using the return statement the function will stop right away to return the value, if we just set the return value as a variable that value is returned when the function ends the normal way: Here comes an example on how to declare and use a function:


    Code:
    Private Sub cmdExample_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExample.Click
        MessageBox.Show(ExampleFunction(5, 4))
    End Sub
    
    Private Function ExampleFunction(ByVal X As Integer, ByVal Y As Integer) As Integer
        Return X + Y
    End Function
    In the example above the value of "ExampleFunction(5, 4)" will be showed in a messagebox. The value of "ExampleFunction(5, 4)" is the value which will be returned from the function when calling it with X=5 and Y=4. The function will simply return the sum of X and Y. So the output of this code will be 9.




    Now when you have learned about sub and functions we can continue with scopes.
    Last edited by Vswe; 03-21-2010 at 03:34 PM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: VB.NET from beginner to advanced programmer Part 13 - Subs and Functions

    +rep!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VB.NET from beginner to advanced programmer Part 18 - Classes
    By Vswe in forum Visual Basic Tutorials
    Replies: 3
    Last Post: 11-02-2009, 06:30 AM
  2. Replies: 1
    Last Post: 11-02-2009, 06:05 AM
  3. Replies: 2
    Last Post: 11-02-2009, 06:01 AM
  4. Replies: 1
    Last Post: 11-02-2009, 05:46 AM
  5. Replies: 1
    Last Post: 11-02-2009, 05:43 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts