// Factorial defined as a constexpr function definition. (4ed page 793). // A function that can be use by the compiler, as well as on run time. // C++11 #include constexpr int fac(int i){ return (i <= 0) ? 1 : i * fac(i - 1); } // compile-time test cases: static_assert(fac(5)==120, "fac(5) problem"); static_assert(fac(10)==3628800, "fac(10) problem"); int main(){ using namespace std; // CALL OF fac AT COMPILE TIME: constexpr int res = fac(10); cout << "fac(10) = " << res << endl; // fac(10) = 3628800 cout << "fac(5) = " << fac(5) << endl; // fac(5) = 120 // CALL OF fac AT RUN TIME: int i; cout << "Enter i: "; cin >> i; cout << "fac(2*i) " << fac(2*i) << endl; // Depends in user input of i }