Browse OOP reference
Paradigms in practice

How does functional programming compare with OOP?

On this page

A functional design often represents variation using data and functions over that data. An object-oriented design often assigns a common operation to several implementations. Adding a new shape or a new calculation stresses these arrangements differently. Many systems use pure calculations inside objects that own mutable resources.

Worked example

Python
from dataclasses import dataclass

def discounted(cents, rebate):
    return max(0, cents - rebate)

@dataclass(frozen=True)
class Voucher:
    rebate: int
    def apply(self, cents):
        return discounted(cents, self.rebate)

assert Voucher(120).apply(100) == discounted(100, 120) == 0

Python Functional Programming HOWTO

Explore class relationships in the OOP Playground

When is object-oriented programming the wrong choice?What is object-oriented programming?What are object-oriented databases?

All OOP questions

Search ObjectFAQ