Search code examples
octave

funtion return [ret] for ret to be 2d


I found this octave function, that returns a tuple in octave:

function [ret] = g(x)
    ret(1, 1) = cos(x)
    ret(1, 2) = sin(x)
end

I don't get the brackets why for [ret], as we are returning one variable, can you explain please? Because ret is a vector, and why vector inside a vector.


Solution

  • The square brackets there don’t form a vector, they collect the output variables. It is part of the function signature.

    function [ret] = … is exactly the same as function ret = …. That is, the brackets are optional when there is a single return variable.

    The same is true when there are no return variables, function [] = name(…) is the same as function name(…).