主要内容

Create Functions in Files

脚本和函数都使您可以通过将其存储在程序文件中来重复使用命令序列。脚本是最简单的程序类型,因为它们完全像您在命令行中输入的命令一样存储命令。功能提供了更大的灵活性,主要是因为您可以传递输入值并返回输出值。例如,此功能名称factcomputes the factorial of a number (n) and returns the result (f).

功能f = fact(n) f = prod(1:n);end

This type of function must be defined within a file, not at the command line. Often, you store a function in its own file. In that case, the best practice is to use the same name for the function and the file (in this example,fact.m), since MATLAB®将程序与文件名相关联。将文件保存在当前文件夹或Matlab搜索路径上的文件夹中。

您可以使用适用于使用MATLAB安装的功能的相同语法规则从命令行调用该函数。对于实例,计算阶乘5。

x = 5;y =事实(5)
y = 120

Starting in R2016b, another option for storing functions is to include them at the end of a script file. For instance, create a file namedmystats.m有几个命令和两个函数,factperm. The script calculates the permutation of (3,2).

x = 3; y = 2; z = perm(x,y)功能p = perm(n,r) p = fact(n)/fact(n-r);end功能f = fact(n) f = prod(1:n);end

Call the script from the command line.

mystats
z = 6

Syntax for Function Definition

The first line of every function is the definition statement, which includes the following elements.

功能keyword (required)

Use lowercase characters for the keyword.

Output arguments (optional)

如果你的函数returns one output, you can specify the output name after the功能keyword.

功能myOutput = myFunction(x)

如果你的函数returns more than one output, enclose the output names in square brackets.

功能[一,二,三] = myfunction(x)

如果没有输出,则可以省略它。

功能myFunction(x)

Or you can use empty square brackets.

功能[] = myFunction(x)

功能名称(必需)

Valid function names follow the same rules as variable names. They must start with a letter, and can contain letters, digits, or underscores.

Note

To avoid confusion, use the same name for both the function file and the first function within the file. MATLAB associates your program with thefilename, not the function name. Script files cannot have the same name as a function in the file.

Input arguments (optional)

如果您的功能接受任何输入,请在功能名称之后将其名称封闭在括号中。带有逗号的单独输入。

功能y = myFunction(one,two,three)

如果没有输入,则可以省略括号。

Tip

When you define a function with multiple input or output arguments, list any required arguments first. This ordering allows you to call your function without specifying optional arguments.

Contents of Functions and Files

The body of a function can include valid MATLAB expressions, control flow statements, comments, blank lines, and nested functions. Any variables that you create within a function are stored within a workspace specific to that function, which is separate from the base workspace.

Program files can contain multiple functions. If the file contains only function definitions, the first function is the main function, and is the function that MATLAB associates with the file name. Functions that follow the main function or script code are called local functions. Local functions are only available within the file.

End Statements

Functions end with either anendstatement, the end of the file, or the definition line for a local function, whichever comes first. Theend如果:

  • Any function in the file contains a nested function (a function completely contained within its parent).

  • The function is a local function within a function file, and any local function in the file uses theendkeyword.

  • The function is a local function within a script file.

尽管有时是可选的,但使用endfor better code readability.

See Also

相关话题