Your first Octave function
In general, the syntax for a function is:
function [output1, output2, ...] = functionname(input1,input2,...) do something (body) endfunction
where output1, output2, ..
. are the output variables generated by the function and input1, input2, ..
. are inputs to the function and are also referred to as input arguments. The function has a name specified by functioname
. The commas separating the outputs are optional. Both output and input arguments are optional and can be scalars, matrices, cell arrays, text strings, and so forth.
Let us first discuss a simple example. Our first function will perform a simple task; it will evaluate the minimum and maximum values of a vector array. We design the function such that the user enters an array and the function then returns the minimum and maximum values. Recall that the maximum and minimum values of an array can be obtained through the Octave functions max
and min
.