Functional Programming Concepts in Plain Language
Functional programming has a reputation for being academic, full of unfamiliar words and mathematical notation. In practice, the core ideas are simple, and many of them are already creeping into mainstream languages. You can use them in Python, JavaScript, Java, and nearly anything else, whether or not the language calls itself functional.
This article explains the main concepts in everyday terms. The goal is not to convert you to a style, but to give you a few tools that make code easier to reason about.
The Big Picture
Most programming you first learn is imperative: you give the computer a list of steps and it changes variables along the way. Functional programming leans on a different idea. You build programs by combining functions that take values in and give new values back, and you avoid changing things in place.
The payoff is predictability. When a piece of code does not secretly change anything else, you can understand it by looking at it alone.
Pure Functions
A pure function has two properties. First, given the same input, it always returns the same output. Second, it has no side effects: it does not modify outside variables, write to files, print to the screen, or otherwise change the world.
def add_tax(price, rate):
return price + price * rate
This is pure. Call it with the same numbers a thousand times, and you get the same answer every time, with nothing else affected. Compare it with a function that reads the current time or updates a global counter. Those are impure, because their results or effects depend on something beyond the arguments.
Pure functions are easy to test, since you only need inputs and expected outputs. They are easy to reuse and safe to run in any order. Real programs need side effects, since printing and saving are the whole point. The functional approach is to keep the impure parts small and push most logic into pure functions.
Immutability
Immutable data cannot be changed after it is created. Instead of modifying a value, you make a new one with the change applied.
original = (1, 2, 3)
extended = original + (4,)
The tuple original is untouched, and extended is a new tuple. Why prefer this? Because shared, changeable data causes confusing bugs. If two parts of a program hold the same list and one edits it, the other sees a change it did not expect. When data cannot change, that whole category of surprise disappears.
Immutability can look wasteful, since you keep creating new values. Many languages and libraries reduce the cost by sharing unchanged parts between old and new versions. Even if you do not enforce it strictly, the habit of avoiding in-place modification where practical makes code clearer.
Functions as Values
In functional style, functions are ordinary values. You can store them in variables, pass them to other functions, and return them from functions. A function that takes or returns another function is called a higher-order function.
Three of these appear everywhere:
- map applies a function to every item in a collection and gives back the results.
- filter keeps only the items for which a test function says yes.
- reduce (or fold) combines all the items into a single value, such as a total.
numbers = [1, 2, 3, 4, 5, 6]
squares = list(map(lambda n: n * n, numbers))
evens = list(filter(lambda n: n % 2 == 0, numbers))
from functools import reduce
total = reduce(lambda acc, n: acc + n, numbers, 0)
Python programmers often write the same ideas as list comprehensions, such as [n * n for n in numbers]. The idea is the same: describe what you want from the collection, not how to loop through it step by step.
Composition
Small functions can be chained so that the output of one feeds the next. This is composition. A task like "take a list of orders, keep the paid ones, get their totals, and add them up" becomes a short pipeline of small, named steps. Each step is simple and can be tested separately.
This pipeline style reads like a description of the task, which makes the intent clearer than a loop with several variables changing at once.
Recursion Instead of Loops
Pure functional languages often avoid loops that change counters, since changing a counter is a side effect. They use recursion instead, where a function calls itself on a smaller problem. In everyday languages you can use loops freely. Knowing the recursive view still helps when you work with nested data such as trees.
Closures and Partial Application
A closure is a function that remembers the values around it when it was created. This lets you build customized functions.
def make_multiplier(factor):
def multiply(x):
return x * factor
return multiply
double = make_multiplier(2)
print(double(10))
Here double is a new function that remembers factor is 2. Creating specialized versions of general functions this way avoids repetition.
Using the Ideas Without Going All In
You do not need to adopt functional programming entirely. A balanced approach works well.
- Write small pure functions whenever you can, and keep input and output at the edges.
- Avoid changing shared data when a new value would do.
- Use map, filter, and similar tools when they make intent clearer, and use a plain loop when that is clearer.
- Do not chase cleverness. Code that nobody can read is not improved by being functional.
Learning a language built around these ideas, such as Haskell, Elixir, or Clojure, is a good way to see them in their purest form, and the lessons carry back to your everyday language.
Frequently Asked Questions
Is functional programming better than object-oriented programming?
Neither is better in every case. They are different ways to organize code, and many projects combine both. Functional ideas are especially helpful for data transformation and for reducing hidden state.
Do I need a functional language to use these ideas?
No. Pure functions, immutability, and higher-order functions can be used in most mainstream languages. A functional language simply makes them the default and enforces them more strictly.
What is a side effect?
A side effect is any change a function makes beyond returning a value, such as modifying a global variable, writing to a file, or printing output. Keeping side effects limited and well-contained makes programs easier to understand.