Functions
A free lesson from Swipe Coding · C++, level 1 · updated October 2026
By the Swipe team, founded by Colin · how we make lessons
A C++ program is built out of "functions" — named, reusable blocks of code that perform a specific task. Instead of writing the same instructions over and over, you can define a function once and then "call" it wherever you need that task done.
int addNumbers(int a, int b) {
return a + b;
}Every function has a few key parts: a return type (what kind of value it hands back, such as "int" for a whole number, or "void" for nothing at all), a name, a list of parameters in parentheses (the inputs it accepts), and a body in curly braces { } containing the statements it runs.
What is a function in C++?
A function that does not return any value uses the return type "void". For example, a function whose entire job is to print something to the screen usually returns "void", since it doesn't need to hand any value back to whoever called it.
void greet() {
std::cout << "Hello there!";
}A function that does not return any value uses the return type ____.
A function can be reused by calling it multiple times, instead of rewriting its code each time.
Every C++ program is a collection of one or more functions. But there is exactly one function that every single C++ program must have — the one where the program actually begins running. That function is called main(), and it's the subject of the next lesson.
Keep your streak going in Swipe Coding
Swipe through every level of C++ on your iPhone — with XP, stars, streaks, a personal advisor and the full track beyond these free levels.
Get the Swipe apps →