Functions

A function bundles a piece of work behind a name so you can reuse it without repeating yourself. You define it once with named inputs (parameters), then call it as often as you like, passing in actual values (arguments); it runs and usually returns a result.

define area(width, height)      parameters: width, height
    return width * height       hand back the product

area(3, 4)     call it with arguments 3 and 4 → returns 12

Functions are how big programs stay manageable: each one is a small, testable idea, and complex behaviour is built by composing them. Passing values in through arguments and getting one back through a return keeps each function self-contained — you can understand it without reading the rest of the program.

Deep dive · How a call actually works: the call stack

When a function is called, the computer can't just jump to it and forget where it came from — it has to remember how to get back. It does this with the call stack: each call pushes a new stack frame holding that call's variables, its arguments, and the return address (where to resume afterwards). When the function returns, its frame is popped off and control jumps back.

Because it's a stack (last on, first off), this handles nesting perfectly: if a() calls b() which calls c(), the frames stack up a → b → c, then unwind c → b → a. It's also why infinite recursion crashes with a "stack overflow" — each call adds a frame, and the stack has a finite size.

hierarchy prerequisite related

See the full map →