Lesson 44 of 45 · C++
Lesson 8: Functions
Duration: 6 min
Lesson 44 of 45 · C++
Duration: 6 min
cpp\n// Forward declaration\ndouble factorial(int n);\n\nint main() {\n std::cout << \"5! = \" << factorial(5) << '\\n';\n return 0;\n}\n\n// Definition\ndouble factorial(int n) {\n if (n <= 1) return 1.0; // base case\n return n * factorial(n - 1); // recursive case\n}\n\n\nKey points:\n- Default arguments allow callers to omit trailing parameters.\n- inline functions hint the compiler to expand the body at call sites (use inline or define inside a class).\n- constexpr functions can be evaluated at compile time if arguments are constant.\n\n> Tip: Keep functions short – ideally one responsibility – to improve readability and testability.\n\n---\n\n<Alert type="info">The compiler may ignore the inline suggestion; modern optimisers decide automatically.