function factn = factorial2(n)

% FACTORIAL: function to compute factorial n!
% Syntax: factn = factorial(n)
% 
% SHBT Student Tutorial Series, Fall 2004
% Introduction to MATLAB programming
% Presenter: Adrian KC Lee
% 
% Date: August 7, 2004


if n < 0 % if not one non-negative input argument
    error('factorial2 only takes 1 non-negative argument') %Display error message and abort.
end

factn = 1;              % initialise. also 0! = 1.
for k = n:-1:1          % go from n to 1
    factn = factn*k;    % Recursion
end