Results 1 to 6 of 6

Thread: Hi Progammers.. What's the Different Between a Sub and Function?

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    39

    Question

    Hi Programmers,

    Let's discuss something.. one friend ask me this question.. and i seem i can't give a proper answer.

    What's the Different Between a Sub and Function?

    Thanks..

    Cheers..!

    Thomas Tan
    ThOmaS TaN

  2. #2
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    461
    Simple

    A Function RETURNS something.

    Statements are called to perform some process or routine (hence they are called a SUBroutine), while functions are used to go away and calculate values to RETURN.

    Of course you can use a function "like" a subroutine in that what it returns is LOST but that is just VB.

    Also note that if you don't specify a return type for the function it is automatically assumed to be a Variant return type.

  3. #3
    Member
    Join Date
    Nov 1999
    Location
    Manila, Philippines
    Posts
    59


    Function - Returns Value
    Sub Routines - Does not return Values


    sample:

    dim intAns as integer

    'This one is a function since something is returned
    intAns = Msgbox("What Do You Want?",vbYesNo+vbExclamation,"Test")

    'This one is a sub-routine
    msgbox "Did you see the difference", vbQuestion + vbOK, "Test"



    Also, as you can see, a function is called with the arguments enclosed in parenthesis but not in the sub-routine.


    Well, that's just a supporting information to what Gen-X just posted. You can have a better and long explanation of this in the books.
    Mikey
    A/P
    Using VB6 SP4 Enterprise Ed.

  4. #4
    Guest
    Some more clarifying code:

    Code:
    Option Explicit
    
    Private Sub Form_Load()
        Dim res As Long
        
        ' Calling the FUNCTION
        Debug.Print FMultiply(10, 5)
        
        ' Calling the SUB
        SMultiply 10, 5, res
        Debug.Print res
    
        ' Callint the Second Sub
        SMultiply2 10, 5
    End Sub
    
    
    ' This Function Returns a long
    Private Function FMultiply(ByVal a As Integer, ByVal b As Integer) As Long
        FMultiply = a * b
    End Function
    
    ' This Sub Doesn't return anything, BUT changes a variabele!
    Private Sub SMultiply(ByVal a As Integer, ByVal b As Integer, ByRef result As Long)
        result = a * b
    End Sub
    
    ' This Sub Doesn't return anything
    Private Sub SMultiply2(ByVal a As Integer, ByVal b As Integer)
        Debug.Print a * b
    End Sub
    As you can see, it also matters if you pass a parameter ByVal or ByRef

    For more info check out your helpfiles...

  5. #5
    Guest
    All of the answers are correct, as far as they go. Originally, there were only subroutines (Assembler). The command would cause the cpu to push the address of the current line, jump to the newline, and retrieve the saved address at the endsub command.

    Functions were built-in to languages and were called similiarly to functions of current languages....except that you couldn't change them.

    Currently, the actual physical difference is that subroutines don't return values and functions always return values. Conceptually, functions never change the program state, and subroutines always change the program state.

    I was taught that if you passed a variable by reference (ie, sent the variable as an address) to a function, that you should immediately assign that reference to a local value to avoid any chance of contamination....program state change. Subroutines were not only expected to change the program state, they were REQUIRED to change the state.

    Good Luck
    DerFarm


  6. #6
    Member
    Join Date
    Oct 2000
    Location
    Philippines
    Posts
    49
    FUNCTIONS RETURNS VALUES

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