MATLAB includes several special types of functions that behave differently than the ordinary functions we have used so far. The scope of.

Download Report

Transcript MATLAB includes several special types of functions that behave differently than the ordinary functions we have used so far. The scope of.

MATLAB includes several special types of functions that behave
differently than the ordinary functions we have used so far.
The scope of a function is defined as the locations within MATLAB
from which the function can be accessed.
The scope of an ordinary MATLAB function is the current working
directory. Ordinary functions can be called by any other function, as
long as they are in the same directory or in any directory on the
MATLAB path. If the function lies in a directory on the MATLAB
path, then the scope extends to all MATLAB functions in a program,
because they all check the path when trying to find a function with a
given name.
In contrast, the scope of the other function types
1. Sub-function
2. Private function
3. Nested function
are more limited in one way or another.
It is possible to place more
than one function in a single
file. If more than one function
is present in a file, the top
function is a normal or
primary function, while the
ones
below
it
are
subfunctions. The primary
function should have the same
name as the file it appears in.
Subfunctions look just like
ordinary functions, but they
are accessible only to the
other functions within the
same file. In other words, the
scope of a subfunction is the
other functions within the
same file.
Private functions are functions that reside in subdirectories with the
special name private. They are visible only to other functions in the
private directory, or to functions in the parent directory. In other
words, the scope of these functions is restricted to the private directory
and to the parent directory that contains it.
You can create your own private directories simply by creating a
subdirectory called private under the directory containing your
functions. Do not place these private directories on your search path.
Nested functions are functions that are defined entirely within the
body of another function, called the host function. They are visible
only to the host function in which they are embedded and to other
nested functions embedded at the same level within the same host
function.
A nested function has access to any variables defined with it, plus
any variables defined within the host function. The only exception
occurs if a variable in the nested function has the same name as a
variable within the host function. In that case, the variable within the
host function is not accessible.
Note that if a file contains one or more nested functions, then every
function in the file must be terminated with an end statement.
In a large program, there could possibly be multiple functions
(subfunctions, private functions, nested functions, and public
functions) with the same name. When a function with a given name is
called, MATLAB execute them in the following order:1. First, MATLAB checks to see if there is a nested function with the
specified name. If so, it is executed.
2. MATLAB checks to see if there is a subfunction with the specified
name. If so, it is executed.
3. MATLAB checks for a private function with the specified name. If
so, it is executed.
4. MATLAB checks for a function with the specified name in the
current directory. If so, it is executed.
5. MATLAB checks for a function with the specified name on the
MATLAB path. MATLAB will stop searching and execute the first
function with the right name found on the path.