Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2,574
Default 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.
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default 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
  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5,939
Default 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.

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 8,856
Default 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 -



  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default 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
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Public Function Question Carl Excel Worksheet Functions 7 August 31st 06 08:15 PM
Public Function - Color Index carl Excel Worksheet Functions 3 April 7th 06 08:08 PM
Using A Public Function / carl Excel Worksheet Functions 1 April 6th 06 09:13 PM
Public Function Problems Andy123 Excel Discussion (Misc queries) 3 December 27th 05 10:11 AM
Public Function Monty Excel Discussion (Misc queries) 9 December 24th 04 06:35 PM


All times are GMT +1. The time now is 05:14 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"