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.
callstateAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Rey Obrero (Capricorn1)Connect With a Mentor Commented:
Private Function  ' can be called FROM the module it is located

Public Function  ' can be called anywhere in your application, from form, query

also take not that if you place a function in a regular module without Public of Private, it is a public function
0
 
mbizupCommented:
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.
0
 
käµfm³d 👽Commented:
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.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
käµfm³d 👽Commented:
It seems I may be mistaken by the "Function" version. Too much .NET for me, I guess   : \
0
 
callstateAuthor Commented:
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?
0
 
Rey Obrero (Capricorn1)Commented:
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
0
 
callstateAuthor Commented:
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.
0
 
Rey Obrero (Capricorn1)Commented:
callstate, try it so you will know which is right
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
Right from the Help file:

If not explicitly specified using Public, Private, or Friend, Function procedures are public by default.
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Connect With a Mentor Commented:
"Why would you WANT a Private vs a Public function, why not make them all Public?"

Only if ...for some reason, you did not want a certain Function 'visible' from outside the scope of where it exists.  And this is likely only going to matter if more than one person (developer) is working on the project.  And also, in a regular Class Module - that you specifically create (as opposed to Form/Report modules, which are also Class modules) ... Private vs Public becomes more important, because you likely only want certain Functions (aka 'Methods') to be visible external to the Class.

mx
0
 
Rey Obrero (Capricorn1)Commented:
0
 
käµfm³d 👽Commented:
kaufmed says "If you leave off "Private" a function is private by default.
I did, but I recanted later: http:#36499179
0
All Courses

From novice to tech pro — start learning today.