Define
A function is a named block of code. Line 2 reads: it returns an int, it is called area, and it takes two ints, w and h. These are its parameters. The body, line 3, runs only when area is called.
First call
Running the program starts main, on line 5. main gets a frame on the call stack: a piece of memory for its local variables, a and b. They have no values yet.
Line 6 calls area(3, 4). main pauses in the middle of line 6 and remembers where it was. The program jumps to area, and a new frame for this call goes on top of the stack.
The arguments 3 and 4 are copied into the parameters, in order: w = 3 and h = 4. w and h live in area's frame. They are local variables of area: main cannot see them.
Line 3 works out w * h: 3 * 4 is 12. return ends the call and sends 12 back to line 6, where main paused. area's frame comes off the stack, so w and h are gone.
main picks up line 6 where it paused. The call area(3, 4) has turned into the value 12, so a starts life as 12. Then main moves on to line 7.
Second call
Line 7 calls area again, with 5 and 2. The same body runs, but in a brand-new frame: w = 5 and h = 2. Nothing is left over from the first call.
Line 3 runs again: 5 * 2 is 10. return sends 10 back to line 7, the frame comes off the stack, and b starts as 10. Each call gives back its own answer.
Results and rules
Line 8 adds a + b, 12 + 10 = 22, and std::cout prints 22. Then main reaches its closing brace. For main alone, that counts as return 0: the program ends well.
The compiler checks every call against line 2. Write area(3), with an argument missing, and it stops with a compile error: area takes two ints. No program is made, so this mistake never runs.
What happens
;; step 1/10 · define
int area (int w, int h) {
↑ ↑ ↑
return name parameters: a type and a name each
type
3 return w * h; the body: runs only when called
area sits above main: a function must be declared
before the line that calls it
The drawing shows each call's local variables on a tray, its stack frame, and the trays pile up as the call stack. The C++ standard only says that local variables and parameters live until their block or call ends; real compilers keep them in a stack frame or in CPU registers. int parameters are passed by value: the parameter is a new variable initialized with a copy of the argument. In void twice(int x) { x = x * 2; }, calling twice(n) leaves n unchanged. A later lesson shows references, which let a function change the caller's variable.