Featured

What Are the Principles of Functional Programming?

Functional programming, like other programming paradigms, follows different principles or has special characteristics. These are essentially as follows.

 

Principle 1: Functions Are First-Class Objects

Functions are first-class objects (also referred to as first-class citizens). Functions can be assigned to variables like other objects and primitive values, and they can be used as arguments of other functions or as their return value. In nonfunctional programming languages, however, functions are not represented as objects and consequently can’t be treated as such.

 

Principle 2: Functions Work with Immutable Data Structures

Data structures in functional programming are usually unchangeable or are not changed. Rather, operations performed on data structures create new data structures if necessary and return them as results. In purely functional programming languages, for example, once lists or other data structures have been created, they cannot be changed later (or only in a roundabout way).

 

Principle 3: Functions Have No Side Effects

Taking the previous point a bit further, there is also the fact that in functional programming the functions usually have no side effects at all and behave more like mathematical functions. This means that functions in functional programming always return the same result for the same input but do not trigger any side effects. In purely functional languages, side effects are already prevented by the language itself.

 

Principle 4: Functional Programs Are Declarative

Imperative programming is a programming paradigm in which you give the computer very precise individual instructions on how to solve a problem. Imperative programs use explicit loop statements (while loops, for loops, etc.), conditional statements (if-else), and sequences of these.

 

Functional programs, on the other hand, are declarative, meaning that you, as the developer, formulate your program more to say what should be done. As a result, functional programs are usually more readable, more meaningful, and more compact than the equivalent imperative code.

 

The figure below shows how to classify the functional programming paradigm.

 

Principles of Functional Programming

 

Unlike object-oriented programming, functional programming focuses on functions, not objects. For example, JavaScript combines the two programming paradigms. You can structure your program in an object-oriented manner—that is, work with objects—and then program functionally in turn within object methods.

 

Note: Functional programs are usually leaner than the equivalent variant in object-oriented or imperative programming.

 

Functional Programming Languages

Examples of (almost) purely functional programming languages include Haskell, Lisp, and Miranda. JavaScript is not a purely functional programming language. For example, data structures such as arrays can be modified even after they have been defined, whereas in Haskell, for example, this is not allowed.

 

Editor’s note: This post has been adapted from a section of the book JavaScript: The Comprehensive Guide by Philip Ackermann.

Recommendation

JavaScript
JavaScript

Begin your JavaScript journey with this comprehensive, hands-on guide. You’ll learn everything there is to know about professional JavaScript programming, from core language concepts to essential client-side tasks. Build dynamic web applications with step-by-step instructions and expand your knowledge by exploring server-side development and mobile development. Work with advanced language features, write clean and efficient code, and much more!

Learn More
Rheinwerk Computing
by Rheinwerk Computing

Rheinwerk Computing is an imprint of Rheinwerk Publishing and publishes books by leading experts in the fields of programming, administration, security, analytics, and more.

Comments