ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   difference between a private sub and a public function? (https://www.excelbanter.com/excel-discussion-misc-queries/135186-difference-between-private-sub-public-function.html)

Dave F

difference between a private sub and a public function?
 
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

--
A hint to posters: Specific, detailed questions are more likely to be
answered than questions that provide no detail about your problem.

Dave Peterson

difference between a private sub and a public function?
 
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.



Dave F wrote:

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

--
A hint to posters: Specific, detailed questions are more likely to be
answered than questions that provide no detail about your problem.


--

Dave Peterson

Jim Thomlinson

difference between a private sub and a public function?
 
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...
--
HTH...

Jim Thomlinson


"Dave F" wrote:

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

--
A hint to posters: Specific, detailed questions are more likely to be
answered than questions that provide no detail about your problem.


Pete_UK

difference between a private sub and a public function?
 
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

On Mar 16, 6:48 pm, Jim Thomlinson <James_Thomlin...@owfg-Re-Move-
This-.com wrote:
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...
--
HTH...

Jim Thomlinson



"Dave F" wrote:
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


--
A hint to posters: Specific, detailed questions are more likely to be
answered than questions that provide no detail about your problem.- Hide quoted text -


- Show quoted text -




Dave Peterson

difference between a private sub and a public function?
 
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.



Jim Thomlinson wrote:

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...
--
HTH...

Jim Thomlinson

"Dave F" wrote:

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

--
A hint to posters: Specific, detailed questions are more likely to be
answered than questions that provide no detail about your problem.


--

Dave Peterson


All times are GMT +1. The time now is 11:57 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com