difference between a private sub and a public function?

G

Guest

What's a good explanation of the difference between a private sub and a
public function?

And is there such a thing as a public sub and a private function?

Thanks,

Dave
 
D

Dave Peterson

Functions are different from Subroutines in that they return something back to
the caller. Subroutines don't return anything.

Functions or subs can be private or public.

If a function or sub is public, then it can be seen by routines in other
modules. If it's private, then that function or sub can't be seen except in
that module.
 
G

Guest

Private and Public refer to the scope of the routine. Function and Sub refer
to the type of routine.

Lets start with Sub and Function. Subs do things to stuff. Functions
evaluate stuff and return a result. For example sorting is a Sub. It takes
stuff and reorders it in either ascending of desending order. Sum on the
other hand is a Function. It looks at a bunch of numbers and returns the sum
of those numbers. The general rule is that Functions should not cause any
side effect (rules are made to be broken but it is a general rule). It should
not change the stuff that it is evaluating. It would be a bad idea for the
Sum function to sort the values it is looking at.

The format of a Function is
Private Function MyFunction(byval MyArg as integer) as Integer
If you do not specify the return type then a variant is returned by default.
the format of a Sub is
Public Sub MySub(byval MyArg as integer)
Note that there is no return specified as subs do not return values.

*******
Public and Private refer to the scope of the procedure (or variable).
Private routines can only be accessed by other procedures in the same module.
Public routines can be accessed by any procedure in any module. If nothing is
specified then the default is for the procedure to be public ("Public Sub
MySub" and "Sub MySub" both have the same scope). The general rule to follow
is to keep everything as private as you can. It makes debugging a whole lot
easier.

You can use any combination of Sub, Function, Private and Public that you
want. Unless you have good reason to not follwo the general rules then keep
your stuff as private as possible and keep side effects out of your
functions. IMO...
 
P

Pete_UK

One effect of declaring a sub or function as Private is that it will
not be listed when you do Tools | Macro | Macros (or Alt-F8), so it is
not obvious from here that it exists.

Pete
 
D

Dave Peterson

Just this portion...

"Subs do things to stuff. Functions evaluate stuff and return a result....The
general rule is that Functions should not cause any side effect (rules are made
to be broken but it is a general rule). It should not change the stuff that it
is evaluating. It would be a bad idea for the Sum function to sort the values it
is looking at."

I don't think that many would agree with this.

Lots of times, I'll have one subroutine--to start the process and the rest of my
routines are functions that do the real work. Sometimes, the value returned is
a boolean just indicating if the routine was successful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top