Functions
A named, reusable computation with inputs (parameters) and an output (return value). The unit of abstraction and of testing.
Level: beginner
What is it?
A function packages a computation behind a name. Parameters receive the inputs and a return value hands back the result. Each call gets its own local variables in a stack frame, created on call and discarded on return. A pure function depends only on its arguments and has no side effects; an impure one reads or changes state outside itself.
Why does it matter?
Functions let you reason about a program one small piece at a time, reuse logic instead of pasting it, and test a piece without running everything. They are also the mechanism that recursion, callbacks, and methods are built on.
Mental model
A vending machine. You put in arguments, it does its private work, and something comes out. You should not need to open the panel to use it, and it should not reach out and rearrange the room.
How it works
- The signature — name, parameters, and return type — is the contract callers rely on.
- Each call pushes a stack frame holding parameters, locals, and the return address. Returning pops it.
- Prefer pure functions: the same input gives the same output with no hidden state. They are easy to test and safe to run in parallel.
- Side effects (printing, writing files, changing globals) belong at the edges. Keep the core pure.
- Default and keyword arguments make calls readable, but a mutable default such as a list is shared between calls in Python.
- In many languages functions are values: you can pass them to other functions as callbacks, sort keys, or handlers.
Steps
- Caller passes arguments
- A new frame is pushed
- The body runs on locals
- A value is returned
- The frame is popped
Code example: Parameters, defaults, and functions as values
def clamp(value, low=0, high=100):
"""Return value limited to the range [low, high]."""
return max(low, min(value, high))
print(clamp(150)) # 100
print(clamp(-5, low=-10)) # -5
print(sorted(["bb", "a", "ccc"], key=len)) # ['a', 'bb', 'ccc']Real-world usage
- Splitting a large task into named steps
- Callbacks and event handlers
- The unit that unit tests call directly
Common mistakes
- Writing a function that does several unrelated things and cannot be named honestly.
- Hiding side effects, such as mutating an argument, behind an innocent-looking name.
- Using a mutable default argument.
Interview questions
What is a pure function and why do people prefer them?
A pure function returns the same output for the same input and changes nothing outside itself. That makes it easy to test, cache, reason about, and run concurrently.
What happens on the call stack when a function is called?
A frame with the arguments, local variables, and return address is pushed. When the function returns the frame is popped and execution resumes at the return address.
What is a higher-order function?
One that takes a function as an argument or returns one — map, filter, sorted with a key, decorators. It lets you pass behavior the way you pass data.