How to define a function inside an m file




















Type any is similar to an object type in other languages. All types in M derive from type any. A function is a value just like a number or a text value, and can be included in-line just like any other expression.

The following example shows a function which is the value of an Add variable which is then invoked, or executed, from several other variables. When a function is invoked, a set of values are specified which are logically substituted for the required set of input values within the function body expression. Functions can be used recursively. In order to recursively reference the function, prefix the identifier with. A valid function name is like a valid variable name: a sequence of letters, digits and underscores, not starting with a digit.

Functions share the same pool of names as variables. The function body consists of Octave statements. It is the most important part of the definition, because it says what the function should actually do.

For example, here is a function that, when executed, will ring the bell on your terminal assuming that it is possible to do so :. See section String Constants. Once this function is defined, you can ask Octave to evaluate it by typing the name of the function. Normally, you will want to pass some information to the functions you define.

The syntax for passing parameters to a function in Octave is. When the function is called, the argument names are used to hold the argument values given in the call. The list of arguments may be empty, in which case this form is equivalent to the one shown above. To print a message along with ringing the bell, you might modify the beep to look like this:. In most cases, you will also want to get some information back from the functions you define. Here is the syntax for writing a function that returns a single value:.

The symbol ret-var is the name of the variable that will hold the value to be returned by the function. This variable must be defined before the end of the function body in order for the function to return a value.

To prevent obscure errors like this, it is a good idea to always make sure that the return variables will always have values, and to produce meaningful error messages when problems are encountered. For example, avg could have been written like this:.

There is still one additional problem with this function. What if it is called without an argument? Without additional error checking, Octave will probably print an error message that won't really help you track down the source of the error. To allow you to catch errors like this, Octave provides each function with an automatic variable called nargin. Each time a function is called, nargin is automatically initialized to the number of arguments that have actually been passed to the function.

For example, we might rewrite the avg function like this:. Although Octave does not consider it an error if you call a function with more arguments than were expected, doing so is probably an error, so we check for that possibility too, and issue the error message if either too few or too many arguments have been provided.

The body of a user-defined function can contain a return statement. This statement returns control to the rest of the Octave program. A return statement is assumed at the end of every function definition.

Unlike many other computer languages, Octave allows you to define functions that return more than one value. The syntax for defining functions that return multiple values is. The list of return values must have at least one element.

Separate inputs with commas. If there are no inputs, you can omit the parentheses. 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. 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.

Functions end with either an end statement, the end of the file, or the definition line for a local function, whichever comes first. The end statement is required if:. 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 the end keyword.

Although it is sometimes optional, use end for better code readability. Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:. Select the China site in Chinese or English for best site performance.



0コメント

  • 1000 / 1000