Note that
n! = 1 * 2 * ... * (n - 1) * n = (n - 1)! * n
For n>1, the factorial of n is equal to the factorial of n−1 multiplied by n. This way of defining a function through the value of the same function calculated for a changed (most often decreased) value of its argument is called recursion. In other words, we are dealing with a recursive function definition when the same function appears within its definition. Of course, when calculating the factorial of an integer, we cannot call the factorial function infinitely for successive arguments. The algorithm must be realized in a finite number of steps. There must exist some argument value for which, instead of another function call, we get a specific value. In the case of the factorial function, this value is 1, where 1!=1.
We must supplement the initial definition of the factorial function with a condition that stops the recursion, finally obtaining:
n! = 1 * 2 * ... * (n - 1) * n = (n - 1)! * n for n> 1
1! = 1
Implementation of the algorithm in:Pascal, C++, Java, Python, JavaScript
