Functional Programming (FP) is a programming style of building the structure and elements such that computations are treated as the evaluation of mathematical functions and state and mutable data remains unchanged.
Think of it like a recipe where you only combine ingredients to create new dishes, without ever altering the original ingredients themselves.
To understand FP, it’s helpful to contrast it with the more common Imperative Programming (e.g., used in Java, C++, Python scripts).
bake( mix( flour, whisk( egg ) )
.” The focus is on what to do by combining expressions and functions. The original egg and flour remain unchanged; we simply use them to create new things.These are the key concepts that define the style:
This is the most important concept. A function is pure if:
Why it matters: Pure functions are incredibly predictable, easy to test, and avoid bugs caused by unexpected changes elsewhere in your code.
Data is never changed after it’s created. Instead of modifying an existing array or object, you create a new array or object with the desired changes.
Why it matters: This eliminates bugs where one part of the code unexpectedly changes data that another part is using. It also makes programs safer to run in parallel (concurrency).
Functions are treated like any other value (like a number or a string). This means you can:
Example: map
, filter
, and reduce
are classic higher-order functions that you pass other functions to.
// JavaScript example of `map` (a higher-order function)
const numbers = [1, 2, 3];
const doubled = numbers.map(n => n * 2); // Pass a function to `map`
// doubled is a NEW array: [2, 4, 6]. `numbers` is unchanged.
FP encourages you to write code that describes what you want to do, not how to do it step-by-step (which is imperative code).
Building complex programs by combining simple, single-purpose functions. The output of one function becomes the input of the next.
Concept: result = function1( function2(data) )
or more clearly, compose(function1, function2)(data)
.
In summary, Functional Programming is a style of writing code that focuses on pure functions, immutable data, and declarative logic to create software that is more predictable, easier to reason about, and less prone to bugs.
Latest tech news and coding tips.
Infinite scroll is a web design technique where content automatically loads continuously as the user scrolls down…
Imagine moving to a new apartment. Instead of disassembling your furniture, rebuilding pipes, and rewiring…
Imagine you’re the principal of a large school. Every day, students (like buttons, links, or…
You know that thing you do? Where you copy a chunk of code, paste it…
We've all seen them. The developers who seem to effortlessly untangle complex problems, whose code…
If you're building a social, chat, or comment-based mobile app using React Native, protecting your…