Thread: What is a Sub
View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 6,953
Default What is a Sub

Jim provides a good philosophical view of functions and subs, but in my
opinion, it mixes the restrictions imposed by UDF's on functions in general -
which VBA/VB does not. VBA sees them both as procedures and as I said, when
used in VBA, then they are exactly the same except that a function can return
a value.

--
Regards,
Tom Ogilvy



"Jim Thomlinson" wrote:

A sub is a set of instructions used to perform an action. In theory you
should try to write your subs to do one thing. For example you could write a
sub to format a sheet of data, or the sub could protect all of the sheets in
a workbook. When your subs start doing too many things they become very
difficult to debug.

A function on the other hand returns a value. In theory a function should
avoid having side effect such as changing any of the data that it is
accessing. For example Sum is a function which returns the total of all of
the numbers that are passed into it. It does not change any of the numbers
that are passed into it as that would be an unwanted side effect.

Very often your subs will call functions in order to perform the actions
that they must do, but rarely will a function call a sub, as the sub would
produce a side effect.
--
HTH...

Jim Thomlinson


"Abode" wrote:

Im very new to Excel and I've found the ability to program functions very
useful and a good way to give me a break from working while still working. I
see many people here asking questions and getting answers with code that
seems to be a 'sub'. I'd love to start learning how to program them but I
dont know how to implement them into Excel and why they are more useful than
functions (other than the ability to obviously be far more complex) Oddly
enough I didn't quite see this question being asked anywhere. Thank you for
your time

Mark