Skip to main content

Functional Programming

Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/functional-programming

In short

Functional programming is a style of building software from pure functions that avoid changing shared data, making code more predictable and easier to test.

What is functional programming?

Functional programming, often shortened to FP, builds programs mainly out of functions that take inputs and return outputs without affecting anything else. Instead of changing variables and objects step by step, you describe how data is transformed from one form into the next. The approach has roots in mathematics and in languages like Lisp and Haskell, but its ideas are now common in JavaScript, Python, Kotlin, Scala, and Rust.

A few core ideas define the style. Pure functions always return the same output for the same input and have no side effects, such as modifying a global variable or writing to a database. Immutability means data is never changed in place; you create a new, updated copy instead. Higher-order functions, which take or return other functions, such as map, filter, and reduce, let you combine small functions into larger behavior.

An assembly line is a helpful analogy: each station takes an item, does one well-defined job, and passes a new result to the next station, without reaching back to change earlier ones. Because pure functions don't depend on hidden state, they are easy to test, reuse, and run in parallel. React components and state management libraries such as Redux borrow heavily from these ideas.

Functional programming is usually contrasted with object-oriented programming, which bundles changing state and behavior inside objects, and with imperative programming, which spells out each step and updates variables as it goes. These styles are not mutually exclusive, and most real-world code mixes them. Real programs still need side effects, like saving files or calling APIs, so functional code tends to push them to the edges of the program rather than eliminate them.

Key takeaways

  • Functional programming builds software from pure functions without side effects.
  • Data is treated as immutable: create new values instead of changing existing ones.
  • Higher-order functions like map, filter, and reduce combine small functions into bigger ones.
  • Pure code is easier to test, reason about, and run in parallel.
  • It is a style rather than a language feature, and it is often mixed with object-oriented code.

Example

Imperative versus functional style in JavaScriptjavascript
const orders = [
  { item: "book", price: 12, paid: true },
  { item: "lamp", price: 30, paid: false },
  { item: "pen", price: 3, paid: true },
];

// Imperative style: update a variable step by step
let total = 0;
for (const order of orders) if (order.paid) total += order.price;

// Functional style: pure transformations, nothing is modified
const paidTotal = orders
  .filter((order) => order.paid)
  .map((order) => order.price)
  .reduce((sum, price) => sum + price, 0); // 15

Readers ask

What is a pure function?

A pure function always returns the same result for the same arguments and has no side effects, meaning it doesn't change outside variables, modify its inputs, or perform input/output. Math.max is pure, while a function that reads the current time or writes to a database is not.

Is JavaScript a functional programming language?

JavaScript is a multi-paradigm language. It supports functional programming well, with first-class functions, closures, and array methods like map and reduce, but it also supports object-oriented and imperative styles.

What is the difference between functional programming and OOP?

Object-oriented programming groups data together with the methods that change it inside objects. Functional programming keeps data separate and immutable and transforms it with pure functions; many codebases combine both approaches.

Often compared

See also

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings