1 / 8

Types of Function

Types of Function. 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.

vlad
Download Presentation

Types of Function

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Types of Function 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 Sub-function Private function Nested function are more limited in one way or another.

  2. Sub-Functions 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.

  3. Private-Functions 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.

  4. Nested-Functions 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.

  5. Nested-Functions

  6. Functions Evaluation Order 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:- 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. 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.

More Related