- 01Getting Started
- 02Your First Output (cout)
- 03Variables and Types (int/double/string/bool)
- 04Conditionals (if statements)
- 05Loops (for statements)
- 06Writing a Function
- 07Working with Arrays
- 08Loops (while statements)
- 09Pointer Basics
- 10References
- 11Classes (Object-Oriented Programming)
- 12Constructors and Destructors
- 13Inheritance
- 14Using vector (the Standard Library)
- 15String Manipulation (string)
- 16Structs (struct)
- 17Error Handling (try/catch)
- 18The Ternary Operator
- 19The switch Statement
- 20[Applied] Build a Mini Inventory Management System
Writing a Function
This lesson covers the basics of defining a function in C++. It's for anyone searching "C++ how to write a function."
A function bundles logic together under a name so it can be called as many times as you need. You define one in the form return-type functionName(parameters) { ... }.
The example defines a function, std::string greet(std::string name), that takes a name and builds and returns a greeting.
A common early mistake is a mismatch between the declared return type and the actual type of the value you return. C++ checks types at compile time, so a type mismatch produces an error, letting you catch the mistake before you even run the code.
In real projects, bundling the same logic into one function โ rather than copying it to multiple places โ makes fixing and maintaining code much easier.
๐งช This site can't compile or run C++ directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ nothing is sent anywhere).
