What is Functional Programming?

I’m documenting my journey from functional neophyte to (hopefully) functional programmer by writing a series of blog posts on the topic. This is the first post describing what, exactly, the word functional programming means.


Functional programming is a programming paradigm that lives alongside other programming paradigms. None of these paradigms have a precise, unanimous definition or standard, and there is not real agreement on which paradigm is better or worse for building particular types of software.

To describe the functional programming paradigm, it’s helpful to contrast it with the more popular imperative style, which most people are very familiar with.

What is Imperative Programming?

We start by defining the programming paradigm that functional programming is so often compared to: imperative programming. The first episode of LambdaCast describes imperative programming by referencing time:

[A] language that is time-based. So do step one, then do step two, step three.

LambdaCast 1: Overview Of Functional Programming

Wikipedia describes imperative programming by referencing state:

In computer science, imperative programming is a programming paradigm that uses statements that change a program’s state. … Imperative programming focuses on describing how a program operates.

Wikipedia - Imperative programming

Microsoft’s C# Guide describes imperative programming by referencing steps:

a developer writes code that describes in exacting detail the steps that the computer must take to accomplish the goal

Function Programming vs. Imperative Programming (C#)

All of these definitions paint a similar picture of imperative programming: step describing a computation.

Imperative Programming
Imperative programming expresses computation as a sequence of statements that change the state of the program, evolving the program’s state to reach a certain goal.

What is Declarative Programming?

To better understand the spectrum of programming paradigms, we next discuss declarative programming, which sits in stark contrast to imperative programming.

Imperative says how to do it. And declarative says what to do.

LambdaCast 1: Overview Of Functional Programming

According to Wikipedia, declarative programming expresses logic without expressing control flow.

In computer science, declarative programming is a programming paradigm … that expresses the logic of a computation without describing its control flow.

Wikipedia - Declarative programming

If imperative programming is describing how to do something, declarative programming is describing what to do. One well-known and widely used declarative language is SQL. With SQL, each SELECT statement specifies what data to return, and the database’s query optimizer and execution engine interpret the SQL statement to figure out how to return it. Regular expressions are another well-known declarative language. Regular expressions specify what to match, while the actual implementation of the matcher is unimportant.

Stackoverflow naturally has an in-depth explanation of what it means to be a declarative program:

The only attribute that can possibly differentiate a declarative expression from an imperative expression is the referential transparency of its sub-expressions. … Referential transparency requires that the implementation of a function may not access the mutable state that is external to the function. Put simply, the function (implementation) should be pure.

stackoverflow

Taking these definitions together, we can come up with a definition for declarative programming.

Declarative Programming
Declarative programming expresses computation as referentially transparent (pure) expressions, without describing control flow.

What is Functional Programming?

This brings us to functional programming.

In computer science, functional programming is a programming paradigm … that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. It is a declarative programming paradigm, which means programming is done with expressions or declarations instead of statements.

Wikipedia - Functional programming

Functional programming is a form of declarative programming that expresses a computation directly as pure functional transformation of data. A functional program can be viewed as a declarative program where computations are specified as pure functions.

Functional Programming
Functional programming expresses computation as the evaluation of pure function expressions.

Comparing Functional and Imperative Programming

This article concludes with a comparison between functional and imperative programming that highlights what makes functional programming unique. This comparison was derived from Microsoft’s Functional vs Imperative article and Wikipedia’s Comparison of programming paradigms.

Characteristic Imperative Approach Functional Approach
Description Programs as statements that directly change computed state. Treats computation as the evaluation of mathematical functions avoiding state and mutable data.
Main traits Direct assignments, common data structures, global variables. Lambda calculus, compositionality, formula, recursion, referential transparency, no side effects.
Programmer focus How to perform tasks (algorithms) and how to track changes in state. What information is desired and what transformations are required.
State changes Important. Non-existent.
Order of execution Important. Low importance.
Primary flow control Loops, conditionals, and function (method) calls. Function calls, including recursion.
Primary manipulation unit Instances of structures or classes. Functions as first-class objects and data collections.

I hope this article made clear what functional programming is. Next time, I will go over why functional programming is important.

comments powered by Disqus