It is never valid to copy and paste a "function" definition to the command line. It is also not valid to eval() a character vector that contains a "function" definition.
In releases up to R2015a, it was never valid to have a "function" definition inside a script file (a .m file in which the first executable word was not "function" and not "classdef"). In R2015b, it became legal to define functions inside of script files, provided that they were after the script, and provided that every "function" definition ended with a corresponding "end" statement.
Whether in functions or scripts, there are places where it is not valid to have a function definition. For example it is not valid to have a function definition inside a for loop or if statement.
if true
function result = test
result = 5;
end
end
Best Answer