Sub / Function: What is the difference?

  • Thread starter Thread starter Teddy
  • Start date Start date
T

Teddy

Could anybody tell me what is the difference between a sub and a
function? For me it seams to be the same.

Thanks
Teddy
 
Simple answer:
Function returns a value, Sub(routine) doesn't.

So:

Private Function ThisFunction(inVal as Integer) as String ' Function returns
a String as its result.
....
....
....
End Function

Private Sub ThisSub(inVal as Integer) ' You can't declare a return type for
a Sub
....
....
....
End Sub

thisSub("Test") ' When calling a sub, you don't expect to get anything back.
newString = ThisFunction("test") ' When calling a Function, you (usually)
assign its return value to somthing.


Cheers,

ChrisM


PS.
In some ways, it's easier to understand the difference in languages like C

In that you declare them both in exactly the same way, but you just specify
a return type of 'void' if is not going to return a value (ie it is a Sub
not a Function):

Private String ThisFunction(Integer inVal) // ThisFunction has a return
type of 'String'
{
}

Private void ThisSub(Integer inVal) // ThisSub does not return anything, so
has a return type of 'void'
{
}
 
Back
Top