A reusable block of code that takes zero or more inputs (parameters), performs a specific task, and optionally returns a value. In mathematics, a function is a mapping that assigns each element of set to exactly one element of set .

Context

Functions are fundamental in both programming and mathematics. In Python, a function is defined with the def keyword: def add(x, y): return x + y. Functions promote code reuse, modularity, and abstraction.

See Also