DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • How To Scan and Validate Image Uploads in Java
  • RAML vs. OAS: Which Is the Best API Specification for Your Project?
  • Five Java Books Beginners and Professionals Should Read
  • DevOps Midwest: A Community Event Full of DevSecOps Best Practices

Trending

  • How To Scan and Validate Image Uploads in Java
  • RAML vs. OAS: Which Is the Best API Specification for Your Project?
  • Five Java Books Beginners and Professionals Should Read
  • DevOps Midwest: A Community Event Full of DevSecOps Best Practices
  1. DZone
  2. Coding
  3. Java
  4. Explaining Functional Programming

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.

Martin Farrell user avatar by
Martin Farrell
·
Jan. 09, 17 · Tutorial
Like (21)
Save
Tweet
Share
19.89K Views

Join the DZone community and get the full member experience.

Join For 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.
Functional programming

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

Opinions expressed by DZone contributors are their own.

Trending

  • How To Scan and Validate Image Uploads in Java
  • RAML vs. OAS: Which Is the Best API Specification for Your Project?
  • Five Java Books Beginners and Professionals Should Read
  • DevOps Midwest: A Community Event Full of DevSecOps Best Practices

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: