Function (programming)
A function is a reusable block of code that performs a specific task, accepts input values called parameters, and typically returns an output. Functions are fundamental building blocks of programs, enabling substitution, modularity, and abstraction.
When you call a function by name with arguments, the code inside executes and produces a result—much like how a mathematical function maps inputs to outputs. Functions can have side effects, like modifying assignments to global state or printing to the screen.
Most programming languages support functions with varying degrees of sophistication. Some treat functions as first-class objects that can be passed around like data; others restrict them to traditional subroutine calls. Recursive functions call themselves, solving problems by breaking them into smaller identical subproblems.
Well-designed functions have clear syntax, single responsibility, and predictable outputs. They reduce code duplication, make programs easier to test, and lower cognitive overhead for readers. Anonymous or lambda functions offer inline alternatives for simple operations.
Functions form the conceptual bridge between how humans think about breaking down tasks and how computers execute instructions sequentially.
Related
Parameter (programming), Return statement (programming), Scope (programming), Recursion (programming), Lambda expression (programming), Code reuse