Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons

© 2026 BraveProgrammer. All rights reserved.

  1. Courses
  2. /
  3. C++ Course

Lesson 44 of 45 · C++

Lesson 8: Functions

Duration: 6 min

Functions\n\nFunctions encapsulate reusable logic, promote modular design, and enable recursion. A C++ function has a return type, a name, a parameter list, and an optional function body. You can forward‑declare a function before defining it, which helps resolve circular dependencies.\n\n---\n\n## Example: Declaration, Definition, and Recursion\ncpp\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.

Previous: Lesson 7: Loops – for, while, do‑whileNext: Lesson 9: Parameter Passing – By Value and By Reference