# For example, here is a function that computes the average of the elements of a vector:

For example, here is a function that computes the average of the elements of a vector: function retval = avg (v) retval = sum (v) / length (v); endfunction If we had written avg like this instead, function retval = avg (v) if (isvector (v)) retval = sum (v) / length (v); endif endfunction and then called the function with a matrix instead of a vector as the argument, Octave would have printed an error message like this: error: retval undefined near line 1 column 10 error: evaluating index expression near line 7, column 1 because the body of the if statement was never executed, and retval was never defined. 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: function retval = avg (v) retval = 0; if (isvector (v)) retval = sum (v) / length (v); else error (“avg: expecting vector argument”);