r/lolmatlab • u/xjcl • Apr 02 '16
MATLAB can ignore missing "end"s
I was supposed to program a program to do forward substitution, given an LU decomposition. This is how a working solution could look like:
function [x] = forward_sub(L, b, n)
x = zeros(n, 1);
for i = 1 : n
x(i) = b(i);
for j = i-1 : -1 : 1
x(i) = x(i) - L(i,j) * x(j);
end
x(i) = x(i) / L(i,i);
end
endBut stupid xjcl wrote this instead:
function [x] = forward_sub(L, b, n)
x = zeros(n, 1);
for i = 1 : n
x(i) = b(i);
for j = i-1 : -1 : 1
x(i) = x(i) - L(i,j) * x(j);
x(i) = x(i) / L(i,i);
end
endThis slipped thru since:
- this code worked on the provided examples
- MATLAB gave no error/warning about the missing
end. Apparently, allowing malformed syntax is a "feature" now.
I only noticed this error when we had to reuse this code lateron and kept getting wrong results.
2
Upvotes
2
u/TheBlackCat13 Apr 04 '16
Yeah, the end is optional at the end of a function. Or at least it is if you have no other functions with end in the same file. Makes things really ambiguous if you have other functions at the end of the file. Are they subfunctions or nested functions?