Defining a function
int square(int x) {
return x * x;
}
int main() {
cout << square(5) << endl;
return 0;
}
A function's return type is declared before its name; return sends a value back to the caller. A function declared void returns nothing at all.
Void functions
void greet(string name) {
cout << "Hello, " << name << "!" << endl;
}
int main() {
greet("Ada");
return 0;
}
Arrays
An array is a fixed-size, ordered collection -- its size is part of its declaration:
string fruits[3] = {"apple", "banana", "cherry"};
cout << fruits[0] << endl; // apple -- indexing starts at 0
Looping over an array
for (int i = 0; i < 3; i++) {
cout << fruits[i] << endl;
}
Why This Matters for Your Exams
Unlike a Python list, a C++ array's size is fixed at declaration, and C++ does nothing to stop you reading or writing past its bounds -- no error, just undefined (and often wrong) behavior. Always declare arrays large enough, and track their length separately instead of guessing.
Try it yourself
lock
Real-time practice is a paid add-on
Reading units and the practice quiz are free. Pay once to unlock running real code for CPP.

