Functional Programming
- In Turkish
- Fonksiyonel programlama
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, andreducecombine 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
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); // 15Readers 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
- FunctionProgramming Fundamentals, p. 21A function is a named, reusable block of code that performs a specific task, optionally taking inputs called parameters and returning a result.
- ClosureProgramming Fundamentals, p. 10A closure is a function that remembers the variables from the scope where it was created, so it can keep using them even after the outer function has returned.
- OOPProgramming Fundamentals, p. 40OOP, or object-oriented programming, is a way of structuring code around objects that bundle related data together with the functions that act on that data.
- RecursionProgramming Fundamentals, p. 48Recursion is a technique in which a function solves a problem by calling itself on smaller versions of the same problem until it reaches a simple base case.
- ReactWeb Development, p. 36React is an open-source JavaScript library for building user interfaces from reusable components that update automatically when their data changes.
- ConcurrencyProgramming Fundamentals, p. 12Concurrency is a program's ability to make progress on several tasks in overlapping time periods, such as serving many users at once rather than one at a time.
Spotted a mistake or something missing on this page?Suggest an edit