Functional Programming (FP) is a programming paradigm where programs are built using functions as the main building blocks, focusing on immutability, pure functions, and avoiding shared state.

Although C++ is mainly Object-Oriented, modern C++ (C++11 and later) supports functional programming concepts like lambdas, higher-order functions, and immutability.


1️⃣ What is Functional Programming?

Functional Programming is a style where:

  • Computation is done using functions
  • Data is not modified (immutable)
  • Functions do not depend on external state

Idea:

Input → Function → Output

Example:

int add(int a, int b)
{
return a + b;
}

No side effects. Same input → same output.


2️⃣ Key Concepts of Functional Programming

ConceptMeaning
Pure FunctionsNo side effects
ImmutabilityData cannot change
First-class functionsFunctions treated like variables
Higher-order functionsFunctions taking functions as arguments
Lambda expressionsAnonymous functions
RecursionReplacing loops
Function compositionCombining functions

3️⃣ Pure Functions

A pure function:

  • Always returns the same result for same inputs
  • Does not modify external variables

Example:

int square(int x)
{
return x * x;
}

Example usage:

cout << square(5); // 25

Bad example (not pure):

int total = 0;int add(int x)
{
total += x; // modifies global state
return total;
}

Why pure functions are good:

  • Easy testing
  • Predictable behavior
  • Safe for parallel computing

4️⃣ Immutability

Immutability means data cannot change after creation.

Example:

const int x = 10;

Instead of modifying data:

Bad:

x = x + 5;

Functional style:

newX = x + 5

In C++:

int x = 10;
int y = x + 5;

Original value stays unchanged.

Benefits:

  • Thread safety
  • No unexpected modifications
  • Easier debugging

5️⃣ First-Class Functions

In functional programming:

👉 Functions can be treated like variables.

They can be:

  • stored in variables
  • passed to other functions
  • returned from functions

Example in C++:

#include <functional>int add(int a, int b)
{
return a + b;
}int main()
{
std::function<int(int,int)> f = add; cout << f(3,4);
}

Output:

7

6️⃣ Lambda Expressions

Lambda = anonymous function

Syntax:

[capture](parameters) { body }

Example:

auto add = [](int a, int b)
{
return a + b;
};cout << add(3,5);

Output:

8

Used heavily in:

  • STL algorithms
  • threading
  • callbacks

7️⃣ Higher-Order Functions

A higher-order function:

  • takes a function as input
  • or returns a function

Example:

#include <iostream>
#include <functional>void execute(std::function<void()> task)
{
task();
}int main()
{
execute([]()
{
std::cout << "Hello\n";
});
}

Output:

Hello

8️⃣ Recursion

Functional programming often replaces loops with recursion.

Example factorial:

int factorial(int n)
{
if(n == 1)
return 1; return n * factorial(n - 1);
}

Call:

cout << factorial(5);

Output:

120

9️⃣ Function Composition

Combining functions.

Example:

f(x) = x + 2
g(x) = x * 3g(f(x)) = (x + 2) * 3

In C++:

int add2(int x)
{
return x + 2;
}int mul3(int x)
{
return x * 3;
}int result = mul3(add2(5));

Output:

21

🔟 Functional Features in Modern C++

C++11 and later introduced functional tools:

FeaturePurpose
Lambda expressionsAnonymous functions
std::functionStore functions
std::bindBind arguments
AlgorithmsFunctional operations
autoType deduction

Example using STL algorithm:

#include <vector>
#include <algorithm>vector<int> v = {1,2,3,4};for_each(v.begin(), v.end(),
[](int x)
{
cout << x << endl;
});

1️⃣1️⃣ Map / Filter Concept (Functional Style)

Common FP pattern.

Example: multiply numbers.

vector<int> nums = {1,2,3,4};for_each(nums.begin(), nums.end(),
[](int x)
{
cout << x * 2 << endl;
});

Output:

2
4
6
8

1️⃣2️⃣ Advantages of Functional Programming

AdvantageExplanation
Predictable codeNo side effects
Easy testingPure functions
Parallel executionImmutable data
Less bugsNo shared state
Modular codeSmall functions

1️⃣3️⃣ OOP vs Functional Programming

OOPFunctional
ObjectsFunctions
Mutable dataImmutable data
ClassesPure functions
InheritanceComposition
State basedStateless

Example:

OOP:

Car object
state changes

Functional:

drive(car_state) → new_state

1️⃣4️⃣ Languages Focused on Functional Programming

Languages designed for FP:

  • Haskell
  • Lisp
  • Scala
  • Erlang
  • Elixir
  • F#

But modern languages support FP features:

  • C++
  • Python
  • JavaScript
  • Rust
  • Kotlin

⭐ Summary

Functional programming focuses on:

Pure functions
Immutability
Higher-order functions
Recursion
Lambda expressions
Function composition

Modern C++ combines three paradigms:

Procedural programming
Object-Oriented Programming
Functional Programming