Functional programming is a programming style where you treat computation as the
evaluation of mathematical functions and avoid changing state or mutating data.
Instead of telling the computer how to do something step-by-step, you tell it what
you want by composing pure functions.
Core Ideas in Functional Programming
Concept Meaning Example
Pure functions Functions that always return the same output for the same inputs
and don’t cause side effects. f(x) = x * 2
Immutability Data doesn’t change; you create new data instead of modifying
existing ones. Instead of updating a list, create a new list with the change.
First-class functions Functions can be assigned to variables, passed as
arguments, or returned. map, filter
Higher-order functions Functions that take other functions as parameters or return
them. map(fn, arr)
Function composition Combine small functions to make bigger ones. h(x) =
f(g(x))
No side effects Avoid modifying external variables, writing to files, etc.,
inside a function (unless necessary).
Basic Example in JavaScript
javascript
Copy
Edit
// Pure function
const double = x => x * 2;
// Higher-order function
const map = (fn, arr) => arr.map(fn);
// Using immutability
const numbers = [1, 2, 3];
const doubled = map(double, numbers);
console.log(numbers); // [1, 2, 3] (unchanged)
console.log(doubled); // [2, 4, 6]
Basic Example in Python
python
Copy
Edit
# Pure function
def double(x):
return x * 2
# Higher-order functions
numbers = [1, 2, 3]
doubled = list(map(double, numbers))
print(numbers) # [1, 2, 3]
print(doubled) # [2, 4, 6]
Steps to Start Writing Functional Programs
Break your program into small functions – each does one thing.
Avoid changing variables – use new variables for new values.
Use built-in functional tools – like map, filter, reduce.
Chain and compose functions – instead of deeply nested loops.
Pass functions as arguments – for reusable logic.