C++ Programming Tutorial

Learn modern C++ step-by-step with concise explanations and practical examples. Navigate topics from basics to advanced features using the sidebar.

Functions

Functions encapsulate logic. You declare a return type, name, and parameters. C++ supports overloading and default parameters. Pass large objects by `const&` to avoid copies.

Function Basics
#include \n\nint add(int a, int b) { return a + b; }\n\n// Overload\ndouble add(double a, double b) { return a + b; }\n\n// Pass by reference\nvoid increment(int& x) { ++x; }\n\nint main(){\n    std::cout << add(2,3) << "\n";\n    std::cout << add(2.5,3.5) << "\n";\n\n    int v = 41;\n    increment(v);\n    std::cout << v << "\n";\n    return 0;\n}
Tips
  • Declare functions in headers and implement in `.cpp` for larger projects.
  • Use references for mutation, `const&` for read-only large inputs.

Keep Practicing

Use the online compiler to run examples and test variations. Reinforce learning by building small programs for each topic.