Programming and Data Types |

You can call function M-files from either the MATLAB command line or from within other M-files. Be sure to include all necessary arguments, enclosing input arguments in parentheses and output arguments in square brackets.

This section provides the following information on calling MATLAB functions:

- "Function Name Resolution"
- "What Happens When You Call a Function"
- "Creating P-Code Files"
- "How MATLAB Passes Function Arguments"
- "Function Workspaces"

**Function Name Resolution**

When MATLAB comes upon a new name, it resolves it into a specific function by following these steps:

- Checks to see if the name is a variable.
- Checks to see if the name is a
*subfunction*, a MATLAB function that resides in the same M-file as the calling function. Subfunctions are discussed in the section, Subfunctions. - Checks to see if the name is a
*private function*, a MATLAB function that resides in a*private directory*, a directory accessible only to M-files in the directory immediately above it. Private directories are discussed in the section, Private Functions. - Checks to see if the name is a function on the MATLAB search path. MATLAB uses the first file it encounters with the specified name.

If you duplicate function names, MATLAB executes the one found first using the above rules. It is also possible to overload function names. This uses additional dispatching rules and is discussed in the section, How MATLAB Determines Which Method to Call.

**What Happens When You Call a Function **

When you call a function M-file from either the command line or from within another M-file, MATLAB parses the function into pseudocode and stores it in memory. This prevents MATLAB from having to reparse a function each time you call it during a session. The pseudocode remains in memory until you clear it using the `clear`

function, or until you quit MATLAB.

You can use `clear`

in any of the following ways to remove functions from the MATLAB workspace.

Syntax |
Description |

`clear function_name` |
Remove specified function from workspace |

`clear functions` |
Remove all compiled M-functions |

`clear all` |
Remove all variables and functions |

**Creating P-Code Files**

You can save a preparsed version of a function or script, called P-code files, for later MATLAB sessions using the `pcode`

function. For example,

parses `average.m`

and saves the resulting pseudocode to the file named `average.p`

. This saves MATLAB from reparsing `average.m`

the first time you call it in each session.

MATLAB is very fast at parsing so the `pcode`

function rarely makes much of a speed difference.

One situation where `pcode`

does provide a speed benefit is for large GUI applications. In this case, many M-files must be parsed before the application becomes visible.

Another situation for `pcode`

is when, for proprietary reasons, you want to hide algorithms you've created in your M-file.

**How MATLAB Passes Function Arguments**

From the programmer's perspective, MATLAB appears to pass all function arguments by value. Actually, however, MATLAB passes by value only those arguments that a function modifies. If a function does not alter an argument but simply uses it in a computation, MATLAB passes the argument by reference to optimize memory use.

**Function Workspaces**

Each M-file function has an area of memory, separate from the MATLAB base workspace, in which it operates. This area is called the function workspace, with each function having its own workspace context.

While using MATLAB, the only variables you can access are those in the calling context, be it the base workspace or that of another function. The variables that you pass to a function must be in the calling context, and the function returns its output arguments to the calling workspace context. You can however, define variables as global variables explicitly, allowing more than one workspace context to access them.

Basic Parts of a Function M-File | Checking the Number of Function Arguments |