I previously talked about what functional programming is by comparing it to other programming paradigms. This post expands on that post to talk specifically about practical differences between functional programming and the paradigm most of us are intimately familiar with — imperative. This post is punctuated with some quotes from the book An Introduction to Functional Programming Through Lambda Calculus.

It’s worth noting that each of these practical differences are enabled because of the power of referential transparency.


Names and Values

Imperative programs are structured as successive assignments of values to variable names. Each assignment updates or changes the value of a named variable, allowing the variable name to refer to a different value at different points in time.

Functional programs are structured as successive nested functional calls. Each function call returns a new value, and that value can be used as a parameter for the next function call. There is no concept of reassigning a variable to a new value

ImperativeFunctional
The same name may be associated with different values.A name is only ever associated with one value.

Execution Ordering

Because imperative programs are structured as successive assignments of values to variables, the order in which those assignments take place is crucial to the outcome of the program.

In pure functional programs, assignment to variables are not allowed. This means that for programs structured as nested function calls, it does not matter the order in which those functions are executed.

ImperativeFunctional
Fixed command execution order.No fixed execution order.

Repetition

In imperative programs, commands are repeated several times (usually within a for-loop) to update the value of a variable.

In functional programs, names cannot be reused or updated, so recursion and function call nesting is used to repeat commands.

ImperativeFunctional
New values may be associated with the same name through command repetition.New values are associated with names through recursive function call nesting.

Data Structures

In imperative programs, data structures are updated by changing the value of fields (or parts) of a structure, leaving the rest intact.

Functional programs do not allow this partial assignment. Functional programs require the data structures you use be explicitly represented using the program itself using recursive notation.

ImperativeFunctional
Data structures represented as changes to state.Explicit representations for data structures.

Functions as Values

In imperative programs, it is rare (though not unheard of) to allow entire method calls to be returned from a method.

In functional programs, functions may freely operate on functions as parameters, and return new functions as return values.

ImperativeFunctional
Functions and methods are special case constructs.Allow functions to be treated as ordinary values.