Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Explaining Functional Programming

DZone's Guide to

Explaining Functional Programming

This quick overview covers how functional programming differs from object-oriented programming, with examples, and how functions can work as first-class citizens.

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

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:

f(x) -> some function of x


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.

public class SideEffectExample {
   private static final Logger LOGGER = Logger.getLogger(SideEffectExample.class.getName());
   boolean changeState = false;
   int i = 1;
   public int setI(int i) {
       this.i = i;
       this.changeState = true;
       LOGGER.info("changee");
       return i + 1;
   }
}


  • 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.

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

Topics:
functional programming ,object oriented programming ,java ,tutorial ,functions ,lambdas

Published at DZone with permission of Martin Farrell, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}