Results 1 to 5 of 5

Thread: Subs and Functions

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 1999
    Location
    Sydney NSW Australia
    Posts
    23

    Smile

    Is there any difference between using a sub or a function when adding a new procedure?

  2. #2
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Boulder, Colorado, USA
    Posts
    325
    a function usually returns data and can be used in logic statement like:

    Code:
    If (IsNumeric(v)) Then
      '' blah
    End If
    where IsNumeric returns a Boolean value
    -Shickadance

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    A sub will only process arguments within it
    A Function can also return a value so you can handle the function name as a value while a sub must be handled like a statement. Using Call keyword you can also call a function without returning a value
    Call Functionname
    So Functions are more advanced while Subs are faster.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    Don't worry about the speed difference at all, a Sub is some code to run and a function is a way of working something out

    Code:
    Public Function CircleArea(Radius As Single) As Double
    
         CircleArea =  3.14159265358979 * Radius * Radius
    
    End Function

    Would let you work out the area of a circle eg

    Code:
    Private Sub Command1_Click
    
    MsgBox "The area of a Circle Radius " & Text1.Text & " is " & CircleArea(Text1.Text) & "."
    
    End Sub
    CircleArea is a Function because it returns the area of a circle a Subroutine eqivilent could Be

    Code:
    Public Sub CircleArea(Radius As Single) As Double
    
         Label1.Caption = (3.14159265358979 * Radius * Radius)
    
    End Sub
    Which actually Does something to your code, but is less versitile than the function, Just think what the proceture's for and then decide.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 1999
    Location
    Sydney NSW Australia
    Posts
    23

    Cool thanks

    thanks a lot for the help people

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