[Solved] Differences between return function in python and Matlab


The use of the return keyword is different in a Python function vs. a MATLAB function.

In a MATLAB function, the Left Hand Side (LHS) arguments define what needs to be defined within the function body prior to returning.

function y = foo(x)
    y = 1;
 end

If you use a return statement to end execution of the function early, you still are responsible for correctly populating the LHS argument list of the function

function y = foo(x)
    y = 1;
    return;
    y = 2;
end

In this example, foo returns 1 as y because the function body defines y to be 1, before execution returns to the caller after the return statement.

In python, the return statement ends execution AND also defines what (if any) values are returned by a function, there is no output argument list in a python function definition.

def foo(x):
    return 1;

Because you need to use the return keyword in python to return values from a function, return tends to be a more heavily used construct than return in MATLAB function definitions.

solved Differences between return function in python and Matlab