?
Solved

Function, Private Function, and Public Function

Posted on 2011-09-07
12
Medium Priority
?
346 Views
Last Modified: 2012-05-12
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.
0
Comment
Question by:callstate
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 1000 total points
ID: 36499144
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
 
LVL 61

Expert Comment

by:mbizup
ID: 36499155
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36499176
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
What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36499179
It seems I may be mistaken by the "Function" version. Too much .NET for me, I guess   : \
0
 

Author Comment

by:callstate
ID: 36499299
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
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 36499376
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
 

Author Comment

by:callstate
ID: 36499392
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
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 36499430
callstate, try it so you will know which is right
0
 
LVL 75
ID: 36499444
Right from the Help file:

If not explicitly specified using Public, Private, or Friend, Function procedures are public by default.
0
 
LVL 75

Assisted Solution

by:DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 1000 total points
ID: 36499465
"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
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 36499468
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36499509
kaufmed says "If you leave off "Private" a function is private by default.
I did, but I recanted later: http:#36499179
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question