Simple Function Definition
Functions help organize code into reusable blocks. They can take parameters and return values.
int add(int a, int b) {return a + b;}int result = add(10, 5);
Hand-picked snippets across TypeScript, Python, Rust, Go, and more — each one comes with a line-by-line breakdown so you can read it like the people who wrote it.
Functions help organize code into reusable blocks. They can take parameters and return values.
int add(int a, int b) {return a + b;}int result = add(10, 5);
Conditional statements allow your program to make decisions based on certain criteria.
int score = 85;if (score >= 50) {std::cout << "Passed";} else {std::cout << "Failed";}
C++ is a strongly typed language, meaning you must declare the type of data a variable will hold.
int age = 25;double price = 19.99;char grade = 'A';bool isReady = true;
Every C++ program starts with a main function. We use #include <iostream> to allow us to print text to the console.
#include <iostream>int main() {std::cout << "Hello, World!" << std::endl;return 0;}
Arrays are used to store multiple values of the same type in a single variable.
int grades[3] = {90, 85, 70};std::cout << grades[0]; // Prints 90grades[1] = 95; // Updates the second element
A for loop repeats a block of code a specific number of times. It is ideal when you know exactly how many iterations are needed.
for (int i = 0; i < 5; ++i) {std::cout << "Count: " << i << std::endl;}
C++ uses namespaces to organize code and avoid naming conflicts. Most standard features are prefixed with 'std::' to indicate they belong to the Standard Library.
std::cout << "Hello from the standard namespace!";
The 'const' keyword creates a read-only variable. Once initialized, its value cannot be changed, which prevents accidental modifications.
const double PI = 3.14159;// PI = 3.14; // This would cause a compiler error