C++ recursive factorial

Recursion needs a base case and progress toward it. The function accepts 0 through 12, keeping the result within the minimum range guaranteed for unsigned long.

C++
#include <iostream>
#include <stdexcept>
#include <cassert>
unsigned long factorial(int n) {
    if (n < 0 || n > 12) throw std::out_of_range("use 0 through 12");
    if (n == 0) return 1;
    return static_cast<unsigned long>(n) * factorial(n - 1);
}
int main() {
    std::cout << factorial(4) << '\n';
    assert(factorial(0) == 1);
}
Read the full lesson