Solved

Function, Private Function, and Public Function

Posted on 2011-09-07
12
342 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 250 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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 250 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

726 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