Is there any difference between using a sub or a function when adding a new procedure?
Printable View
Is there any difference between using a sub or a function when adding a new procedure?
a function usually returns data and can be used in logic statement like:
where IsNumeric returns a Boolean valueCode:
If (IsNumeric(v)) Then
'' blah
End If
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.
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
CircleArea is a Function because it returns the area of a circle a Subroutine eqivilent could BeCode:Private Sub Command1_Click
MsgBox "The area of a Circle Radius " & Text1.Text & " is " & CircleArea(Text1.Text) & "."
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.Code:
Public Sub CircleArea(Radius As Single) As Double
Label1.Caption = (3.14159265358979 * Radius * Radius)
End Sub
thanks a lot for the help people