8.10.1 Function Scope | |
A function's scope defines where it is visible and callable from. There are 3 levels of function scope, PUBLIC , EXPORT and PRIVATE :
Visibility | |||
---|---|---|---|
Type | File | Modules | User |
PUBLIC | Yes | Yes | Yes |
EXPORT | Yes | Yes | no |
PRIVATE | Yes | no | no |
A PUBLIC function is visible everywhere - to functions in the same file, to other library modules and scripts, and to the user, ie. it can be the entry function to the script. (Modules are discussed in the section Vortex nbsp;Library nbsp;Modules .)
An EXPORT function is visible to other functions in the same file, and to other library modules and scripts, but not to the user. Thus, an attempt to start the script at such a function will default to main , as if the function did not exist. EXPORT functions are useful as semi-private util functions that are shared among script programmers, but hidden from casual end users.
A PRIVATE function is visible only to the file it is in. Other modules that may be linked in cannot call the function; indeed they could redeclare another function with the same name. PRIVATE functions are visible only to the file they're in. They provide security not only from end users, but from other modules as well. This makes them useful for file-specific functions that shouldn't ever collide with shared function names.
A script function's scope is declared right after the NAME attribute. If no scope is given, the function will generally be PRIVATE by default (though not always; see the manual on Function Scope ). User and builtin functions are always PRIVATE .
Because PRIVATE and EXPORT functions cannot be seen by the end user, it is wise to always make functions have the lowest possible scope. This prevents access to a script from an unintended and possibly unprotected point by the user.
Back: Functions | Next: Function Parameters |