Results 1 to 4 of 4

Thread: Difference between Sub and Function in Visual Basic and VB.Net

  1. #1
    Join Date
    Dec 2008
    Posts
    9

    Difference between Sub and Function in Visual Basic and VB.Net

    What's the difference between a Sub and a Function in Visual Basic ?

  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    re: Difference between Sub and Function in Visual Basic and VB.Net

    A Function can return a value from itself. For example

    Code:
    Public Function Sum(ByVal a As Integer, _
    ByVal b As Integer) As Integer
    Return a + b
    End Function
    Then you can use this Function as follows

    Dim result As Integer = Sum(2, 4)

    On the other hand a Sub can not return a value from itself. It must use a
    ByRef parameter to get a result. For example

    Code:
    Public Sub SumSub(ByVal a As Integer, _
    ByVal b As Integer) As Integer)
    Dim theSum As Integer = a + b
    End Sub
    This has no way of returning the result and using this like

    Dim answer As Integer = SumSub(2, 4)

    will generate a complile time error.

  3. #3
    Join Date
    Jan 2008
    Posts
    1,521

    Sub Vs Function in Visual Basic

    The Sub statement is used to execute one or more lines of code, period.

    The Function statement is used to execute one or more lines of code AND return a value to the caller.

    Use a Sub when you do not need to return a value to the calling code. Use a Function when you need to return a value.

  4. #4
    Join Date
    Apr 2008
    Posts
    1,948

    Sub and Function in Visual Basic.Net

    To learn more about Visual Basic.NET Sub and Function statements click these links:

    Sub
    Function

Similar Threads

  1. Difference between Visual Basic and Access
    By Well-Tech in forum Windows Software
    Replies: 4
    Last Post: 07-09-2010, 04:09 PM
  2. Is GUI same like Visual Basic ?
    By Caesar in forum Software Development
    Replies: 2
    Last Post: 02-03-2009, 01:32 PM
  3. Visual Basic 2005 or Visual Basic 6
    By Aasha in forum Software Development
    Replies: 5
    Last Post: 15-01-2009, 06:56 PM
  4. Visual Basic 2008 & VB.net Difference any?
    By samit in forum Software Development
    Replies: 4
    Last Post: 05-08-2008, 07:44 PM
  5. Visual Basic on LAN
    By djbbenn in forum Software Development
    Replies: 2
    Last Post: 05-08-2008, 02:15 PM

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
  •  
Page generated in 1,713,575,053.82230 seconds with 17 queries