Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

function

Declare function name, inputs, and outputs

Syntax

  • function [y1,...,yN] = myfun(x1,...,xM)
    example

Description

example

function [y1,...,yN] = myfun(x1,...,xM) declares a function named myfun that accepts inputs x1,...,xM and returns outputs y1,...,yN. This declaration statement must be the first executable line of the function. Valid function names begin with an alphabetic character, and can contain letters, numbers, or underscores.

You can save your function:

  • In a function file which contains only function definitions. The name of the file should match the name of the first function in the file.

  • In a script file which contains commands and function definitions. Functions must be at the end of the file. Script files cannot have the same name as a function in the file. Functions are supported in scripts in R2016b or later.

Files can include multiple local functions or nested functions. For readability, use the end keyword to indicate the end of each function in a file. The end keyword is required when:

  • Any function in the file contains a nested function.

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

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

Examples

collapse all

Define a function in a file named average.m that accepts an input vector, calculates the average of the values, and returns a single result.

function y = average(x)
if ~isvector(x)
    error('Input must be a vector')
end
y = sum(x)/length(x); 
end

Call the function from the command line.

z = 1:99;
average(z)
ans =
    50

Define a script in a file named integrationScript.m that computes the value of the integrand at $2\pi/3$ and computes the area under the curve from 0 to $\pi$. Include a local function that defines the integrand, $y = \sin(x)^3$.

% Compute the value of the integrand at 2*pi/3.
x = 2*pi/3;
y = myIntegrand(x)

% Compute the area under the curve from 0 to pi.
xmin = 0;
xmax = pi;
f = @myIntegrand;
a = integral(f,xmin,xmax)

function y = myIntegrand(x)
y = sin(x).^3;
end
y =

    0.6495


a =

    1.3333

Define a function in a file named stat.m that returns the mean and standard deviation of an input vector.

function [m,s] = stat(x)
n = length(x);
m = sum(x)/n;
s = sqrt(sum((x-m).^2/n));
end

Call the function from the command line.

values = [12.7, 45.4, 98.9, 26.6, 53.1];
[ave,stdev] = stat(values)
ave =
   47.3400
stdev =
   29.4124

Define two functions in a file named stat2.m, where the first function calls the second.

function [m,s] = stat2(x)
n = length(x);
m = avg(x,n);
s = sqrt(sum((x-m).^2/n));
end

function m = avg(x,n)
m = sum(x)/n;
end

Function avg is a local function. Local functions are only available to other functions within the same file.

Call function stat2 from the command line.

values = [12.7, 45.4, 98.9, 26.6, 53.1];
[ave,stdev] = stat2(values)
ave =
   47.3400
stdev =
   29.4124

Related Examples

Introduced before R2006a

Was this topic helpful?