Thread: Sub vs Function

    #1
  1. No Profile Picture
    Demonic Swordsman DGQB
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Apr 2003
    Posts
    1,043
    Rep Power
    83

    Sub vs Function


    Hi,

    Can we create functions (not subs) in VB? If so, what is the diff between FUNCTION and SUB?

    private sub cmd_click
    private function cmd_click
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Windsor ON, Canada
    Posts
    462
    Rep Power
    13
    you can create functions in vb. however, an event is not a function. it is always a sub. the difference between a sub and a function is a function can return a value.
    Code:
    Private Function Multiply(X As Integer, Y As Integer) As Integer
        ' inside the function you can use the function
        ' name as a variable. this is how you return
        ' a value from a function.
        Multiply = X * Y
    End Function
    
    ' an example call
    Dim product As Integer
    product = Multiply(5, 4)
  4. #3
  5. No Profile Picture
    Demonic Swordsman DGQB
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Apr 2003
    Posts
    1,043
    Rep Power
    83
    the difference between a sub and a function is a function can return a value.
    LOL, is that the only diff between sub and function? As I vaguely remember, other modern languages like java do not differentiate sub/function/method?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Windsor ON, Canada
    Posts
    462
    Rep Power
    13
    well functions are a little slower. "modern" languages do have a sub/function difference only they do not use a different word for it. you would specify void for a sub that doesn't return a value and any other data type if it does return a value. that isn't an issue of being "modern" as vb.net uses the sub and function keywords. it's all a mater of syntax and BASIC vs C.

IMN logo majestic logo threadwatch logo seochat tools logo