Results 1 to 9 of 9

Thread: diff between function and sub?

  1. #1
    PORRASTAR
    Guest

    diff between function and sub?

    What is the difference between a function and a sub ?

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    a function returns a value, a sub normally does not.. is that good enough ?
    -= a peet post =-

  3. #3
    PORRASTAR
    Guest
    In which circumstance should I use a function and when should I use a sub?

  4. #4
    Lively Member
    Join Date
    Sep 2001
    Location
    Berkshire
    Posts
    121
    I think a function performs a task, and then always returns a value. Also with a function, it must be passed some arguments. A sub does not have either of these two requirements. A sub can either be called (without being passed any arguments) or triggered on an event (such as a mouse click).

    This is by no means a computer science definition, more just an observation of mine.

  5. #5
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527

  6. #6
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527

  7. #7
    Hyperactive Member
    Join Date
    Jun 2000
    Posts
    350
    Originally posted by Darren
    Also with a function, it must be passed some arguments.
    A function doesn't have to have any arguments passed to it:

    VB Code:
    1. Public Function Testing() As Single 'no arguments in the ()
    2.  
    3.     Dim fResult As Single
    4.     fResult = Rnd
    5.     Testing = fResult
    6.  
    7. End Function
    8.  
    9. Private Sub CommandCallFunction_Click()
    10. TextHello = Testing  ' no argument
    11. End Sub
    .

  8. #8
    Addicted Member
    Join Date
    May 1999
    Posts
    161
    Just to confuse the matter ever more... you are perfectly allowed to create a function and never assign the return value... Now, you can really confuse the heell out of people reading your code.

    Is that messed-up or what ?

  9. #9
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Originally posted by FrancisC
    Just to confuse the matter ever more... you are perfectly allowed to create a function and never assign the return value... Now, you can really confuse the heell out of people reading your code.

    Is that messed-up or what ?
    A VB function will always return a value.
    If you haven't explicitly told what to return a default value will be sent back to the caller.
    The default values are zero for numeric data types, and empty string for strings and False for boolean return values.

    Best regards

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