Defining and Calling Functions in C++
A function packages reusable logic under a name: a return type, a name, a parameter list, and a body. C++ requires declaration before use — define helpers above main() or declare prototypes (signatures ending in ;) up top and define bodies later. Calling runs the body with the arguments bound to parameters, then resumes after the call.
main() itself is just a function with a special role as entry point, returning int as the exit code. Splitting programs into small functions with single responsibilities is the primary tool for taming complexity at every scale.
Signature anatomy
int add(int a, int b) promises: takes two ints, returns an int. The body between braces fulfills the promise.
Declare before use
The compiler reads top to bottom — a function must be declared (prototype) or fully defined before any call to it.
Call and resume
add(2, 3) jumps into the body with a=2, b=3; return hands 5 back and execution continues after the call expression.
void returns nothing
void greet() performs an action with no result to hand back — callers use it as a bare statement.
#include <iostream> int add(int a, int b) { return a + b;} int main() { std::cout << add(2, 3) << std::endl; return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C++ yourself — building beats watching.