function factn = factorial(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

factn = 1;          % initialise. also 0! = 1.
for k = n:-1:1      % go from n to 1 (k=1 being the stopping case)
    factn = factn*k;% multiply n by n-1, n-2 etc. This is the actual recursive step.
end

% Question: What happens when n < 0