Main Content

Types of Functions

Local and Nested Functions in a File

Program files can contain multiple functions. Local and nested functions are useful for dividing programs into smaller tasks, making it easier to read and maintain your code.

Local functionsare subroutines that are available within the same file. Local functions are the most common way to break up programmatic tasks. In a function file, which contains only function definitions, local functions can appear in the file in any order after the main function in the file. In a script file, which contains commands and function definitions, local function must be at the end of the file. (Functions in scripts are supported in R2016b or later.)

For example, create a function file namedmyfunction.mthat contains a main function,myfunction, and two local functions,squareMeanddoubleMe:

functionb = myfunction(a) b = squareMe(a)+doubleMe(a);endfunctiony = squareMe(x) y = x.^2;endfunctiony = doubleMe(x) y = x.*2;end

You can call the main function from the command line or another program file, although the local functions are only available tomyfunction:

myfunction(pi)
ans = 16.1528

Nested functions是完全包含在另一个函数。The primary difference between nested functions and local functions is that nested functions can use variables defined in parent functions without explicitly passing those variables as arguments.

Nested functions are useful when subroutines share data, such as applications that pass data between components. For example, create a function that allows you to set a value between 0 and 1 using either a slider or an editable text box. If you use nested functions for the callbacks, the slider and text box can share the value and each other’s handles without explicitly passing them:

functionmyslider value = 0; f = figure; s = uicontrol(f,'Style','slider','Callback',@slider); e = uicontrol(f,'Style','edit','Callback',@edittext,...'Position',[100,20,100,20]);functionslider(obj,~) value = obj.Value; e.String = num2str(value);endfunctionedittext(obj,~) value = str2double(obj.String); s.Value = value;endend

Private Functions in a Subfolder

Like local or nested functions, private functions are accessible only to functions in a specific location. However, private functions are not in the same file as the functions that can call them. Instead, they are in a subfolder namedprivate. Private functions are available only to functions in the folder immediately above theprivatefolder. Use private functions to separate code into different files, or to share code between multiple, related functions.

Anonymous Functions Without a File

Anonymous functions allow you to define a function without creating a program file, as long as the function consists of a single statement. A common application of anonymous functions is to define a mathematical expression, and then evaluate that expression over a range of values using a MATLAB®function function, i.e., a function that accepts a function handle as an input.

For example, this statement creates a function handle namedsfor an anonymous function:

s = @(x) sin(1./x);

This function has a single input,x. The@operator creates the function handle.

You can use the function handle to evaluate the function for particular values, such as

y = s(pi)
y = 0.3130

或者,你也可以通过函数句柄函数that evaluates over a range of values, such asfplot:

range = [0.01,0.1]; fplot(s,range)

Related Topics