Sobes.tech
Junior — Middle

How do functional programming approaches differ from object-oriented programming?

sobes.tech AI

Answer from AI

Functional programming (FP) and object-oriented programming (OOP) differ in their approach to code organization and state management.

  • FP focuses on pure functions that have no side effects and always return the same result for the same inputs. Data is considered immutable. Programs are built as compositions of functions.

  • OOP focuses on objects — entities that combine data and methods to operate on them. Objects can change their state, and interaction occurs through method calls.

Example:

FP (Python):

def add_one(x):
    return x + 1

result = add_one(5)  # always 6

OOP (Python):

class Counter:
    def __init__(self):
        self.count = 0
    def increment(self):
        self.count += 1

c = Counter()
c.increment()
print(c.count)  # 1

Thus, FP is oriented towards computations and data transformations without changing state, while OOP is focused on modeling entities with state and behavior.