You are on page 1of 16

2/15/24, 11:08 PM What Is Functional Programming and Why Use It?

- Coding Dojo

Bootcamp 101

Tech Tips

Career Guides

Tech Trends

Student
Success

What Is Functional Programming and Why Use It?


By Brad Mitchell / July 13, 2022

If you’ve just started learning how to code, deciding which programming paradigm and programming
language to learn first can be tricky.

If you want to master a programming paradigm relevant to today’s tech landscape, consider going with
functional programming. It’s a simplified, cleaner, and more predictable way to create code. Your resulting
code is also easier to test and maintain.

https://www.codingdojo.com/blog/what-is-functional-programming 1/16
2/15/24, 11:08 PM What Is Functional Programming and Why Use It? - Coding Dojo

But what exactly is functional programming? Read on to learn more about this programming paradigm, its
advantages, and the most popular functional programming languages.

What Is Functional Programming?


Functional programming (FP) is an approach to software development that uses pure functions to create
maintainable software. In other words, building programs by applying and composing functions.

Functional programming harnesses language support by using functions as variables, arguments, and return
values—creating elegant and clean code in the process. FP also uses immutable data and avoids concepts like
shared states. This is in contrast to object-oriented programming (OOP), which uses mutable data and shared
states.

Functional programming languages focus on declarations and expressions rather than the execution of
statements. Functions are also treated like first-class citizens—meaning they can pass as arguments, return
from other functions, and attach to names.

FP focuses on the results, not the process, while iterations like loop statements and conditional statements
(e.g., If-Else) aren’t supported.

FP evolved from the lambda calculus (λ-calculus), a simple notation for functions and applications that
mathematician Alonzo Church developed in the 1930s. Many programming languages and dialects use the
functional paradigm, including Scheme, Common Lisp (CL), and Elixir.

Many of today’s top programming languages—including C#, Java, JavaScript, PHP, and Python—support
programming in a functional style or use features found in FP.

The following section will discuss the difference between pure and impure functions in functional
programming.

Pure Functional Programming

Purely functional programming is a subset of FP that treats all functions as deterministic mathematical or pure
functions.

In deterministic mathematical functions, the development of future states of the system does not allow any
randomness. Pure functions, meanwhile, have function return values that are identical for identical arguments.
The function application also has no side effects (i.e., it does not modify state variable values outside its local
environment).

A side effect occurs in a program when you insert external code into your function. This prevents the function
from properly performing its task. Impure functions, in contrast, contain one or more side effects.

https://www.codingdojo.com/blog/what-is-functional-programming 2/16
2/15/24, 11:08 PM What Is Functional Programming and Why Use It? - Coding Dojo

To see pure functions in action, refer to the following JavaScript code:

function updateMyAddress(newAddress) {

const myAddress = [“ChurchSt”, “CovingtonCross”];

myAddresses[myAddresses.length] = newAddress;

return myAddresses;

updateMyAddress() doesn’t need any external code to accomplish its tasks. This means it’s a pure function.

Impure Functional Programming

To see impure functions in action, check out this JavaScript code:

​const myAddresses = [“ChurchSt.”, “CovingtonCross”];

function updateMyAddress(newAddress) {

myAddresses.push(newAddress);

return myAddresses;

updateMyAddess() is an impure function since it contains code (myAddress). This code mutates an external
state, which gives updateMyAddress() some side effects.

Why Functional Programming Matters


While FP has generally been less popular than object-oriented programming, it has grown in popularity in
recent years due to the rise of machine learning and big data. Functional programming is notable for its ability
to efficiently parallelize pure functions. Code for data analysis workflows and tasks is easier to analyze, test,
and maintain using the functional programming paradigm.

Due to its pure nature, FP is ideally suited for analyzing extensive data sets and machine learning. Pure
functions will always generate the same results, with no outside values to influence the final results.

https://www.codingdojo.com/blog/what-is-functional-programming 3/16
2/15/24, 11:08 PM What Is Functional Programming and Why Use It? - Coding Dojo

Algorithms created using FP can also quickly identify and correct errors. Many programmers and software
developers would rather work with a programming paradigm that is easy to debug because of its pure
functions.

As highlighted in a paper published by John Hughes of the University of Glasgow, functional programming
plays a crucial role in future tech development because of its modularity. Modularity breaks down large and
complex projects into simpler modules. You can test the modules separately, which lessens the amount of
time spent on unit testing and debugging.

“Functional programming languages provide two new kinds of glue — higher-order functions and lazy
evaluation. Using these glues, one can modularize programs in new and useful ways,” Hughes notes in his
paper.

Advantages of Functional Programming


Modularity – As previously mentioned, functional programming is highly modular. This makes the
resulting code shorter and easier to read. Anyone who has tried to decipher monolithic code would
appreciate the simplicity.
You can implement lambda calculus in the program – You can use this to solve complex problems.
Contains many functional constructs – These include lazy map, lazy evaluation, and lists.
Some programming languages support nested functions – This significantly improves the
maintainability of the code.
Problems are easier to pinpoint and solve – FP’s reliance on pure functions makes debugging and unit
testing easier. Pure functions also prevent confusing issues and errors from developing in the code.
Keeps concurrency safe – Code is thread-safe when no two concurrent processes try to access the same
data simultaneously. This bug is a race condition. Since pure functions never share a state with other
sections of the program, race conditions can’t occur.

The 7 Core Functional Programming Concepts


To help you better understand the problems that functional programming can solve, let’s look at the seven
core functional programming concepts.

1. Pure Functions

We’ve already established that pure functions are deterministic and have no side effects. However, as noted in
an article published in Hackernoon by Victor Cordova, the goal is not to create code that is completely devoid
of side effects.

“It’s important to understand we don’t want to eliminate all side effects since all programs need to do some
sort of side-effect such as calling APIs or printing to some stdout. What we want is to minimize side effects,
so our program’s behavior is easier to predict and test,” Cordova said.

https://www.codingdojo.com/blog/what-is-functional-programming 4/16
2/15/24, 11:08 PM What Is Functional Programming and Why Use It? - Coding Dojo

In the following code written by Cordova, a side effect (sessionIsActive) modifies a variable outside its
scope. This is causing problems for the function caller.

Alt Text=”code written using the functional paradigm with side effect”

Source: Hackernoon

In the second screenshot, it’s modified code, and the side effect is gone. The function caller can now perform
the task without any issues.

https://www.codingdojo.com/blog/what-is-functional-programming 5/16
2/15/24, 11:08 PM What Is Functional Programming and Why Use It? - Coding Dojo

Alt Text=”code written using the functional paradigm without side effects”

Source: Hackernoon

2. First-Class Functions

Another key benefit of first-class functions is that there are no limits and restrictions on how to use
functions. In other words, they behave like any other variable.

First-class functions lay the groundwork for other modifications, like currying, higher-order functions, and
closures.

3. Higher-Order Functions

Higher-order functions take functions as an argument or return the functions. Aside from modularizing
programs, you can also use higher-order functions to make functions polymorphic (i.e., allows for the use of a
single code multiple times).
https://www.codingdojo.com/blog/what-is-functional-programming 6/16
2/15/24, 11:08 PM What Is Functional Programming and Why Use It? - Coding Dojo

4. Immutability

Immutable objects do not change. In FP, you initialize and implement objects and values without changing
their values or state. You can create new objects and values if necessary, but you can’t modify an existing
object or value’s state.

Functional programming’s immutability aligns with a key principle of mathematics—that objects can’t
change their state. This principle is evident in even the most basic mathematical formulas.

Immutability prevents problems from arising and spreading in your code. In a multithreaded application, a
thread can act on an immutable object without altering the other threads since no one is modifying the object.
In concurrent applications, this prevents errors in your code.

5. Recursion

Recursion takes place when a function calls itself. A recursive function executes code and repeatedly runs the
function until it meets an exit condition. You can find this pattern in a standard loop: the loop declares an
initial variable and executes the code to be done with the variable. This continues until it meets a stopping or
exit condition.

You can use a recursion instead of a loop. However, a loop can never substitute for a recursion.

Here are some key points to using recursive functions in your JavaScript code:

Recursion often gets the job done properly when sorting tree structures (i.e., node relationships).
If you find yourself repeatedly nesting loops within loops, a recursive function can help you extract data
and reuse the function for different trees. For a more in-depth explanation, check out this tutorial on using
recursive functions in JavaScript.

6. Function Composition

Function composition allows you to combine pure functions to create more complicated ones. The same
principle applies in mathematics: the result of one function continues as the argument of the following
function, and the result of the last function is the result of the whole.

In coding, we can combine multiple steps into a single code line or into a new function.

7. Referential Transparency

Referential transparency allows a value to replace its expression in a program (or anything with the same
value). This happens without changing the result of the program. This logic dictates that methods should
always return the same values for given arguments without additional effects.

https://www.codingdojo.com/blog/what-is-functional-programming 7/16
2/15/24, 11:08 PM What Is Functional Programming and Why Use It? - Coding Dojo

Referential transparency makes reasoning about programs a more straightforward process. It renders each
subprogram independent, which dramatically simplifies refactoring and unit testing.

The 6 Most Popular Functional Programming Languages


1. Haskell

Haskell is a general-purpose and purely functional programming language. Every function is a pure function
in the mathematical sense. Statements and instructions are non-existent, and the only available expressions
are those that can’t mutate variables (local or global) nor access state (like random numbers or time).

2. Erlang

Erlang (Erlang/OTP) is a general-purpose, functional, and concurrent programming language. It’s used to
build scalable soft real-time systems that require high availability. Erlang is widely employed in eCommerce,
computer telephony, and instant messaging.

3. Clojure

Clojure is a functional and dynamic dialect of Lisp on the Java platform. It combines a highly organized
infrastructure with the interactive development of a scripting language. Clojure is ideally suited for
multithreaded programming.

4. Common Lisp

Common Lisp is a descendant of the Lisp family of programming languages. It’s ANSI-standardized and
multi-paradigm (supporting a combination of functional, procedural, and object-oriented programming
paradigms). Common Lisp also has a robust macro system that allows programmers to tailor the language to
suit their application.

5. Scala

Scala is a general-purpose programming language that supports both OOP and FP. Static types help prevent
bugs in complex applications, while JavaScript and JVM runtimes allow programmers to build dynamic
systems supported by ecosystems of libraries.

6. Elixir

Elixir is a functional, general-purpose programming language suitable for building scalable and maintainable
applications. It harnesses the Erlang VM, which runs low-latency, fault-tolerant, and distributed systems.
Elixir is widely used in embedded software, web development, multimedia processing, and other
applications.

https://www.codingdojo.com/blog/what-is-functional-programming 8/16
2/15/24, 11:08 PM What Is Functional Programming and Why Use It? - Coding Dojo

Object-Oriented Programming vs Functional Programming


While there are several key differences between object-oriented and functional programming, one is the
imperative versus declarative programming model.

OOP uses the imperative programming model, meaning functions are invariably coded in every step needed
to solve a problem. You code each operation with the code itself specifying how to solve the problem. This
model requires the programmer to know which functions are necessary to solve a problem instead of relying
on models that can solve the problems.

FP uses the declarative programming model, meaning it relies on the underlying concepts of a programming
language to execute the necessary steps to reach the predetermined outcome.

Imperative programs focus on the step-by-step process of solving a problem, whereas declarative programs
focus on the result of solving a problem.

Another critical difference is mutability: OOP uses mutable data while FP uses immutable data. You can alter
(or mutate) mutable objects after creation, whereas you can’t for immutable objects. In FP, you’ll need to
make a copy of the object and use that copy to write the rest of your code.

Overall, immutable code is easier to update, more efficient to manage, and easier to test and debug. And
because variables are constant, the resulting code is easier to understand and reason about. Many
programmers and software developers prefer to work with FP models.

Ultimately, the right programming paradigm for you will depend on your intended application. OOP works
best for standardized and straightforward projects, whereas FP works best for projects that require scalability
and flexibility.

Master Functional Programming Languages with Coding Dojo


Now that you understand the merits of learning functional programming, learning the applicable
programming languages is the next step.

Coding Dojo offers web and software development bootcamps to help you master the most in-demand
programming languages—namely Java, Python, C#.NET, and MERN. You’ll learn full-stack web
development, which consists of an application’s front-end and back-end portions.

Armed with these skills, you’ll have what it takes to work with the top tech companies and earn excellent
compensation: the average salary for a full-stack developer in the United States was $101,931 in 2022,
according to Indeed.

Submit your application today to launch your new learning and career journey!

https://www.codingdojo.com/blog/what-is-functional-programming 9/16
2/15/24, 11:08 PM What Is Functional Programming and Why Use It? - Coding Dojo

🔍 Search...

Sign Up for Our Newsletter

Get the latest info about:

Course dates

Discounts

Job trends

Email

Sign Up

Start Your New Career Path

Ready to make a change? Here’s how we can help if you apply today:

Personalized support

Flexible courses

https://www.codingdojo.com/blog/what-is-functional-programming 10/16
2/15/24, 11:08 PM What Is Functional Programming and Why Use It? - Coding Dojo

Unlock your potential

Apply Now

Discover More Topics

Software Development

Cybersecurity

Data Science

    

Which course is
right for you?

Looking for a change but


not sure what path is right
for you? Take our custom
quiz to find out which
bootcamp is right for you.

https://www.codingdojo.com/blog/what-is-functional-programming 11/16
2/15/24, 11:08 PM What Is Functional Programming and Why Use It? - Coding Dojo

Take
the
Quiz

 Luke Lappala
 August 29, 2023
From Customer
Success to
Customer
Solutions
Engineer | How
Alumni Danielle G
Advanced Her
Career Path
Coding Dojo alumni
Danielle G went from
working in customer
success to being a
customer solutions
engineer thanks to the
skills she learned in
bootcamp.
Read More

 Luke Lappala
 August 29, 2023
From Project
Manager to Sr.
Data Analyst | How
Alumni Nathalie C
Upskilled to
https://www.codingdojo.com/blog/what-is-functional-programming 12/16
2/15/24, 11:08 PM What Is Functional Programming and Why Use It? - Coding Dojo

Advance Her
Career Path
Coding Dojo alumni
Nathalie C upskilled from
a Project Manager to a Sr.
Data Analyst thanks to the
skills she learned in
bootcamp.
Read More

 Luke Lappala
 August 29, 2023
From Paralegal to
Proposal Writer &
Business Analyst |
How Alumni Adam
K Changed His
Career Path
Coding Dojo alumni Adam
K went from working as a
Paralegal to working as a
Proposal Writer &
Business Analyst thanks
to the skills he learned in
bootcamp.
Read More

 Luke Lappala
 July 27, 2023
From Packing
Packages to
https://www.codingdojo.com/blog/what-is-functional-programming 13/16
2/15/24, 11:08 PM What Is Functional Programming and Why Use It? - Coding Dojo

Programmer | How
Judah M
Accelerated His
Career Path Into
Tech
Disclaimer: Coding Dojo
cannot guarantee
employment, salary, or
career advancement. The
experience of this
alumnus/alumna may not
be representative of all
students. Pre-Dojo:
Worked in a UPS
warehouse and did...
Read More

 Luke Lappala
 July 27, 2023
From Arbitrage to
AI | How Michael G
Followed His
Passion to
Reinvent His
Career Path
Coding Dojo alumni
Michael G went from
working in retail arbitrage
to working as a Solutions
Engineer in the AI field
thanks to the skills he
learned at bootcamp.
Read More

https://www.codingdojo.com/blog/what-is-functional-programming 14/16
2/15/24, 11:08 PM What Is Functional Programming and Why Use It? - Coding Dojo

 Luke Lappala
 July 27, 2023
From Valet to Data
Engineer Intern |
How Chris D
Pivoted and Took
the Leap to
Accelerate His
Career Path in
Tech
Disclaimer: Coding Dojo
cannot guarantee
employment, salary, or
career advancement. The
experience of this
alumnus/alumna may not
be representative of all
students. Pre-Dojo:
Worked as a valet and
was attending...
Read More

DISCLOSURES

Institutional Disclosures

RESOURCES CAMPUSES

Algorithm App Online

https://www.codingdojo.com/blog/what-is-functional-programming 15/16
2/15/24, 11:08 PM What Is Functional Programming and Why Use It? - Coding Dojo

Careers
FAQ
Tech for America
City University
Course Packets

Call Us at (844) 446-3656


from 5 AM to 6 PM PST or, send an email.

Sign-Up for the Latest News & Resources

Email Sign Up

By submitting your information, you agree and accept the Coding Dojo’s Terms of Use, Cookie
Policy and Privacy Policy.

    

©2022 All rights reserved. No information may be duplicated without Coding Dojo’s permission. Coding Dojo is part of
Colorado Technical University. Coding Dojo cannot guarantee employment, salary, or career advancement. Not all
programs are available to residents of all states and certain foreign countries. The appearance of U.S. Department of
Defense (DOD) visual information does not imply or constitute DOD endorsement. See
the Institutional Disclosures section for information on the agencies that approve and regulate the school’s programs.
1575 Garden of the Gods Road, Suite 100 Colorado Springs, CO 80907

https://www.codingdojo.com/blog/what-is-functional-programming 16/16

You might also like