What is functional programming?

Programming Paradigm Concepts

Eleanor Thomas

Senior Data Analytics Engineer

What is functional programming?

  • Functional programming: a programming paradigm involving functions, specifically pure functions
  • Pure functions: a process that takes input values, produces output values based only on the input values, and does not do anything else
  • Separation of responsibilities is achieved in functional programming via functions

Input, Processing, Output

Programming Paradigm Concepts

What is a pure function?

  • Concept of pure function in functional programming comes from mathematics
  • Pure functions only look at input and only produce output
  • Pure functions have no "side effects"
  • No side effects means:
    • No influence on other variables in the program
    • No writing to files
    • No saving information to a database
Programming Paradigm Concepts

Example of a pure function

Pure function

def pure_sum(x, y):
    output = x + y

    return output

Not a pure function

def not_pure_sum(x, y):
    output = x + y

    print(output)

    return output
Programming Paradigm Concepts

Benefits of pure functions

  • Pure functions are easier to understand and debug
  • Testing of pure functions is easier
  • Output for a given input is entirely predictable
    • Similar to mathematical functions: 5 squared is always 25

Input, Output

Programming Paradigm Concepts

Let's practice!

Programming Paradigm Concepts

Preparing Video For Download...