What is Functional Programming? 4 Core Concepts that you must know to get the first class ticket

Tired of formal coding practices? Looking to improve your skills? The way most developers do it is imperative programming. Don’t panic. Simply speaking, Imperative Programming is simply changing a variable directly. But in functional world, you can consider all of your variables as constants. You are not sure to understand what you read? Don’t worry, you’ve just found the world’s easiest tutorial. I won’t use difficult words.

The 4 pillars of functional programming

  1. Mutability
  2. Mutable State
  3. First-Class and Higher Order Functions
  4. Pure Functions

Let’s dig into the subject one by one!

Mutability

There are many different concepts in functional programming and one of the most important ones is mutability, meaning that everything is immutable, you cannot really change anything.

Let’s take a look at imperative programming, this is what you are really familiar with

var age = 30
// some code here
age = 40

You create a variable called age and then you change the variable called age from thirty to forty.

That’s perfectly fine when you are talking about imperative programming, the object oriented programming, the programming that you are familiar with.

But if you try to do the same thing in functional programming, it’s not going to work in functional programming.

This is the wrong example in a functional programming.

var age = 30 
// some code here
age = 45
func doSomething() {
print("Hello World")
age = 67
}
print(age) // age is 45
doSomething()
print(age) // age is 67

This is not going to work in functional programming. Everything and functional programming, the variables that you create are actually constant.

  • They cannot be changed.
  • They are in an immutable state.

So the state of the variables cannot be changed in functional programming.

Mutable State

Let’s talk about mutable state, so the programming that you already know, the object oriented programming, that when you have a mutable state, it means that anyone can change any value, then you will have some issues dealing with concurrency.

Most Common Side Effects are…

  • Race Conditions
  • Dead Locks

Deadlocks means something that is waiting on something and that something is waiting on something else.

All of these things can happen when you are working with the object oriented world or imperative programming.

In functional programming, since everything is in immutable state, meaning you cannot really change it. All of those things just go the way you don’t really have concurrency problems.

  • You don’t have deadlocks.
  • You don’t have things depending on other things.

Everything will be changed beneath them.

First-Class and Higher-Order Functions

Everything in functional programming is a higher order function. It means a function that can take and return another function.

You will se many different example using filter, map, reduce.

All of these different features operators can take in a function and also return a function.

Pure Functions

Everything in functional programming is pure functions. It means that

  • The function will always produce the same output when given the same input
  • The function creates zero side effects outside of it

Once again, the mutability is coming into play. You cannot really change the state. You are not allowed to change the state in functional programming.

Conclusion

  • In functional programming, every variable is immutable.
  • You don’t face the problems with Concurrency Issues with Race Conditions and DeadLock
  • Higher Order Function is a function that can take and return another function.
  • Pure functions will always produce the same output when given the same input.
  • Pure functions create zero side effects outside of it.

This is the most basic concepts for functional programming.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
KD Knowledge Diet

Software Engineer, Mobile Developer living in Seoul. I hate people using difficult words. Why not using simple words? Keep It Simple Stupid!