Matlab Octave Functions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

Internal

Declaration

Create a file with a .m extension and a content similar to:

function [return-variables =] <function-name>([comma-separated arguments])
<body>
end

The "return-variables" section of the function header defines which variables represent the return values of the function. A function may return zero, one or more values.

A function that does not return a value:

function somefunc(...)

A function that returns one value (the value may be a scalar, vector, matrix, etc.):

function result = somefunc(...)

A function that returns multiple values (the values may any combination of scalars, vectors, matrices, etc.):

function [result1, result2] = somefunc(...)

A function may have zero, one or more arguments. The () must be specified even if there are no arguments.

The function body uses the arguments and assigns values to the return variables defined in the function header.

Function Invocation

If it only has one return value:

return-value-var = somefunc(arg1, arg2, ...)

If it has multiple return values:

[return-value-var1, return-value-var2, ...] = somefunc(arg1, arg2, ...)

Function References

... @(t)(myFunction(myArg1, myArg2, ...)) ...

This expression creates a function, with argument t, which calls myFunction().