Skip to content
Devgo

Function

A reusable block of code that takes inputs (parameters), does work, and returns a result. Functions are the primary way to organize and reuse logic. They enable abstraction, modularity, and testing.

When to use
  • Always — functions are fundamental.
When not to
  • Never.

Definition

A function is a named, callable unit of code. You define it with parameters (inputs), it executes statements, and returns a value. Functions can be pure (no side effects) or impure (modify external state). First-class functions can be passed as arguments and returned.

Why it exists

To avoid repeating code, organize logic into testable units, and abstract complexity behind a name and interface.

How it works

Define a function (def, function, fn). Call it with arguments. The function body runs, optionally returns a value. Parameters can be positional, named, or default. Closures capture surrounding . calls itself.

Scaling limits

Deep can stack-overflow. Impure functions are harder to test and reason about. Too many parameters signal poor design.

Cost shape

Free.

Alternatives

  • Inline code — Simpler for one-off logic but no reuse.

Failure modes

  • Side effects making functions hard to test.
  • Too many parameters.
  • Deep stack overflow.

, ,