Results 1 to 2 of 2

Thread: Want to know different between Sub and Function

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2002
    Posts
    1

    Wink Want to know different between Sub and Function

    Hi, I just start learning VB, and one thing confuse me is the Sub and Function, what is different between them, when should I use Sub and when should I use Function or which one is better, are they both the same ?
    Thank You

  2. #2
    Hyperactive Member onerrorgoto's Avatar
    Join Date
    Aug 1999
    Location
    Sweden
    Posts
    330

    A function returns a value

    in its simplest form:
    A sub is used when you want a procedure that doesn't return any value.

    A function returns a value after it has performed it's task.

    Put a textbox nd a button on a form and type this:
    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim strRetVal As String
    3.  
    4.     'call sub
    5.     Call MySub(Text1.Text)
    6.  
    7.     'call function
    8.     strRetVal = MyFunction(Text1.Text)
    9.     MsgBox strRetVal
    10.  
    11. End Sub
    12.  
    13. Public Sub MySub(strSubInput As String)
    14.     Dim strTask As String
    15.  
    16.     strTask = "MySub " & strSubInput
    17.     MsgBox strTask   'Show result
    18. End Sub
    19.  
    20. Public Function MyFunction(StrFunctionInput As String) As String
    21.     Dim strTask2 As String
    22.    
    23.     strTask2 = "MyFunction " & StrFunctionInput
    24.    
    25.     MyFunction = strTask2   'Return result to the calling procedure
    26. End Function
    Last edited by onerrorgoto; Dec 3rd, 2002 at 03:54 AM.
    Onerrorgoto

    Dont be to optimistic, the light at the end of the tunnel might be a train

Posting Permissions

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



Click Here to Expand Forum to Full Width