Results 1 to 9 of 9

Thread: When to use a function or sub?

  1. #1

    Thread Starter
    Addicted Member Eric_B's Avatar
    Join Date
    May 2001
    Location
    home sweet home
    Posts
    130

    When to use a function or sub?

    I know a the difference between a function and a sub, but when do I use function instead of a sub is confusing...

    Best Regards!
    Eric

  2. #2
    nullus
    Guest
    just use a function if you want to return a value. if you just want a bunch of code to do stuff, use a sub.

  3. #3
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    You use a function when you need to return a value. If you don't need anything returned, use a sub.

  4. #4

    Thread Starter
    Addicted Member Eric_B's Avatar
    Join Date
    May 2001
    Location
    home sweet home
    Posts
    130
    or if i only use function. What will happen then?

  5. #5
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    The difference between a subroutine and a function is that the
    name of a subroutine is a way for the subroutine to be identified and called.
    For a function, its name doubles as a variable declaration
    that may be returned to the calling program.

    If you use a function in the following way

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim a As Double
    3. a = test
    4. MsgBox a
    5. End Sub
    6.  
    7. Function test() As Double
    8. MsgBox "hgi"
    9. End Function

    The function executes the msgbox command and returns a double.

  6. #6
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    My point was that a function returns a value even if you treat it like a sub and don't assign a return value, in response to:

    or if i only use function. What will happen then?
    My interpretation was he was asking what would happen if you only ever used functions and not subs.

    I was trying to illustrate (with code rather than words) that even when you use a function like a sub and don't explicitly assign a retrun value, it still returns a value.

    I knew what I was saying
    Last edited by Nucleus; Jun 27th, 2001 at 07:57 PM.

  7. #7
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    LOL.

    I hate writing. Watch how scarce I become if I ever hear the words: help files or documentation !

  8. #8

    Thread Starter
    Addicted Member Eric_B's Avatar
    Join Date
    May 2001
    Location
    home sweet home
    Posts
    130
    thanx for the help, but how would that affect the speed?

  9. #9
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    As a guess, I would say function to do the same thing vs sub, then the sub would use a little less memory and perhaps run a simgde slower.

    In reality it doesn't really matter as other parts of your code will be the performance bottleneck and not this issue.

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