Link to home
Start Free TrialLog in
Avatar of callstate
callstate

asked on

Function, Private Function, and Public Function

Can anyone give me a "layman's" explanation of the differences between:
  Function -
  Private Function -
  Public Function -

Why would you use one over the other ...  I see this in example code and don't understand the differences.
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Private Function - visible only within the module it resides in
Public Function - Visible throughout your application

Function - Depends... if it is in a standard module (in the modules collection), it is visible throughout your application.  If it is in a form's module it is only visible within the context of that form.
Function
Private Function


Are identical in meaning. If you leave off "Private" a function is private by default. A private function is one that is only accessible to the class in which the function is defined.

Public Function
A public function is one that is accessible to any caller, including the class defining the function itself.

In order to call a public function, you must have a reference to an instantiated object, that is, an object for which you said something like:

Set obj = New SomeArbitraryClassName()

Open in new window


Note the use of the "New" keyword.

If your functions occur inside Modules, then you typically won't (if ever) have an instantiated object to call the function against. It would basically be a global function. The same is true for a private function, except that you would only be able to call the private function from within that same module.
It seems I may be mistaken by the "Function" version. Too much .NET for me, I guess   : \
Avatar of callstate
callstate

ASKER

OK ... so PRIVATE means only visible to the code in the same form, module, etc. and PUBLIC means visible across the entire project?  (I am using AC07 VBA)

and ... FUNCTION by itself defaults as Public or Private?

and ... Why would you WANT a Private vs a Public function, why not make them all Public?
callstate,

 did you read my post?

 *also take note that if you place a function in a regular module without Public of Private, it is a public function
capricorn1 says "take note that if you place a function in a regular module without Public of Private, it is a public function"

kaufmed says "If you leave off "Private" a function is private by default.
callstate, try it so you will know which is right
Right from the Help file:

If not explicitly specified using Public, Private, or Friend, Function procedures are public by default.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
kaufmed says "If you leave off "Private" a function is private by default.
I did, but I recanted later: http:#36499179