Explaining Functional Programming

by: Martin Farrell #MVB @glenwareltd

It occurred to me that while I know what functional programming is, I would struggle to explain it. My description would be something like:

“You know how object-oriented programming is about objects? Well, functional programming is about functions. You know lambdas, Haskell, and Scala.”

This post is my attempt to provide a better description of functional programming, so feel free to let me know about any feedback or mistakes I’ve made.

What Is Functional Programming?

Functional programming uses mathematical functions to solve problems. Functions take an input and give an output, without changing the input:

Common Features

  • Functions are first-class entities: They can be assigned to variables, passed as arguments, or returned from other functions.
  • No state.
  • No side effects: Anything the code does except produce an output from given inputs.

Consider the example below the where I change the changeState field, and also the log. They are side effects, as they go beyond returning an output from an input.

  • Lazy evaluation: The compiler can decide when best to run a function.

The degree to which the above features are enforced depends on the language.

Advantages of Functional Programming?

Functional Programming means that a function will give the same output for a given input, and doesn’t alter state. This makes it well suited for problems involving:

  • Concurrency.
  • Multi-threading.
  • Multi-processors.

Learn more about DZone at https://dzone.com