Learning path

Full curriculum

Full curriculum

Unit content

Functions and parameters

A function packages a computation behind a name so that it can be used repeatedly without duplicating its implementation.

A function can receive parameters and produce a return value.

function square(x):
    return x * x

Calling square(5) supplies the argument 5 for the parameter x and returns 25.

Parameters and arguments

A parameter is the name used in the function definition. An argument is the value supplied at a particular call.

Several parameters allow a function to express a family of related computations:

function rectangle_area(width, height):
    return width * height

Return values

A returned value can become part of a larger expression or be stored for later use.

A function may also perform side effects such as writing output or changing external state, but a function whose result depends only on its inputs is often easier to reason about and test.

Function calls create local work

Each call supplies its own parameter values and intermediate local state. Recursive functions can even call themselves, creating a new invocation for each step.

Functions are both a control-flow mechanism and a unit of abstraction: callers can rely on what a function does without needing to repeat how it does it.