Main Content

Private Functions

This topic explains the term private function, and shows how to create and use private functions.

Private functions are useful when you want to limit the scope of a function. You designate a function as private by storing it in a subfolder with the name private. Then, the function is available only to functions and scripts in the folder immediately above the private subfolder.

For example, within a folder that is on the MATLAB® search path, create a subfolder named private. Do not add private to the path. Within the private folder, create a function in a file named findme.m:

function findme
% FINDME  An example of a private function.

disp('You found the private function.')

Change to the folder that contains the private folder and create a file named visible.m.

function visible
findme

Change your current folder to any location and call the visible function.

visible
You found the private function.

Although you cannot call the private function from the command line or from functions outside the parent of the private folder, you can access its help:

help private/findme
  findme  An example of a private function.

Private functions have precedence over standard functions, so MATLAB finds a private function named test.m before a nonprivate program file named test.m. This allows you to create an alternate version of a particular function while retaining the original in another folder.

Related Topics