Sobes.tech
Junior — Middle

Can you explain the concept of functional programming and its main features?

sobes.tech AI

Answer from AI

Functional programming (FP) is a programming paradigm based on the use of functions as the main building blocks. Key features of FP:

  • Pure functions — functions that have no side effects and always return the same result given the same inputs.
  • Immutability of data — data does not change after creation; instead, new copies are created with modifications.
  • Higher-order functions — functions that accept other functions as arguments or return them.
  • Absence of state and side effects — simplifies understanding and testing of code.
  • Recursion instead of loops — recursion is often used for repeating operations.

Example in Python:

def pure_function(x, y):
    return x + y

# Higher-order function

def apply_function(f, value):
    return f(value)

result = apply_function(lambda x: x * 2, 5)  # Will return 10

FP helps in writing more predictable, modular, and easily testable code.