Terminate block of code or indicate last array index
end
end
also terminates a declared function. Although it is sometimes
optional, use end
for better code readability. end
is required in these cases:
If a file contains functions, and one of the functions is terminated with
end
, then every function in the file must be terminated with
end
.
If a file contains a function with one or more nested functions, then every
function in the file must be terminated with end
.
If a script contains one or more local functions, then every function in the file
must be terminated with end
.
end
also represents the last index of an array. For example,
X(end)
is the last element of X
, and
X(3:end)
selects the third through final elements of
X
.
Classes can overload the end
function to implement specialized
behavior. For more information, see end as Object Index.
If an array X
already exists, you can use end
to grow the array. For example, X(end+1) = 5
increases the length of
X
by 1 and adds a new element to the end of
X
.
Although end
is sometimes optional in a function file, use it for
better code readability.