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
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Lesson 8: Functions | C++ Course lesson | BraveProgrammer
  1. Courses
  2. /
  3. C++ Course

Lesson 8 of 45

·C++

Lesson 8: Functions

6 min

Course progress18%

What you'll learn

Keep these ideas in mind as you work through the lesson.

4 points
  • Function declaration vs. definition
  • Default arguments
  • Function prototypes
  • Inline functions
  • Functions

    Functions 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.


    Example: Declaration, Definition, and Recursion

    // Forward declaration
    double factorial(int n);
    
    int main() {
        std::cout << \"5! = \" << factorial(5) << '\
    ';
        return 0;
    }
    
    // Definition
    double factorial(int n) {
        if (n <= 1) return 1.0; // base case
        return n * factorial(n - 1); // recursive case
    }
    

    Key points:

    • Default arguments allow callers to omit trailing parameters.
    • inline functions hint the compiler to expand the body at call sites (use inline or define inside a class).
    • constexpr functions can be evaluated at compile time if arguments are constant.

    Tip: Keep functions short – ideally one responsibility – to improve readability and testability.


    <Alert type="info">The compiler may ignore the inline suggestion; modern optimisers decide automatically.

    Make it stickPractice what you've learned1 quiz · 2 practical tests · Optional activities from C++ CourseBrowse activities
    Previous lessonLesson 7: Loops – for, while, do‑whileContinue to lesson 9Lesson 9: Parameter Passing – By Value and By ReferenceBack to C++ Course curriculum