/ˈfʌŋkʃənl ˈproʊɡræmɪŋ/
noun … “Writing code as evaluations of pure functions.”
Functional Programming is a programming paradigm where computation is expressed through the evaluation of functions, emphasizing immutability, first-class functions, and declarative code. Unlike OOP, which centers on objects and state, Functional Programming avoids shared mutable state and side effects, making reasoning about code, testing, and concurrency more predictable and robust.
Key characteristics of Functional Programming include:
- Pure functions: Functions that always produce the same output given the same input and have no side effects.
- Immutability: Data structures are not modified; operations produce new versions instead of altering originals.
- First-class and higher-order functions: Functions can be passed as arguments, returned from other functions, and stored in variables.
- Declarative style: Focus on what to compute rather than how to compute it, often using recursion or functional combinators instead of loops.
- Composability: Small functions can be combined to form complex operations, enhancing modularity and reuse.
Workflow example: In Scala or Haskell, a developer may process a list of numbers by mapping a pure function to transform each element and then filtering results based on a predicate, without mutating the original list. This approach allows parallel execution and easier debugging since functions do not rely on external state.
val numbers = List(1, 2, 3, 4, 5)
val squaredEven = numbers.map(n => n * n).filter(_ % 2 == 0)
println(squaredEven) // Output: List(4, 16)Conceptually, Functional Programming is like a series of conveyor belts in a factory. Each function is a station that transforms items without altering the original input. The final product emerges predictably, and individual stations can be modified or optimized independently without disrupting the overall flow.
See Scala, Haskell, OOP, Immutability, Higher-Order Function.