DEV Community

Cover image for Functional Programming For Beginners
Kinanee Samson
Kinanee Samson

Posted on • Updated on

Functional Programming For Beginners

Functional programming is a programming paradigm that is focused on composing functions together to execute a particular programming task. Many programming languages available today supports functional programming style. Most people would argue that they rather do things the Object Oriented way but i tell you, there is nothing wrong in using a functional programming approach to writing your code. Due to the support we get with most languages we can pass functions as arguments to to other functions, we can also return a function from another function. So the basic idea about using a functional programming approach is to isolate different processes in our code in to reusable functions. Since functional programming is concerned so much with functions, we will look at what functions are and their types.


Functions are at the core of most programming languages we use today, they serve as a way to isolate any reusable chunk of code. Functions can also be used to house certain programming procedures or tasks, By default a function returns a value which is null if we don't actually return anything from that function. In the world of functional programming there are two types of functions; Pure functions and impure functions.

Pure Functions

Pure functions are functions that don't change the internal state of our code. A pure function will not alter or mutate a value that is declared outside of it's scope and it's internal logic is not dependent on such value. It is only concerned with the arguments that are passed to it. One thing about pure functions is that they should return the same output given the same input.

const add = (a:number, b:number):number => a + b; // PURE FUNCTION
add(2, 3) // 5
const addRand = (a:number) => {
    const rand = Math.random();
    return rand + a;
Enter fullscreen mode Exit fullscreen mode

Like we said above the function addRand is not a pure function because it will not return the same output given the same input. Any function that make calculations based on random numbers are not pure functions. However we see that the first function will always return the same output 5 as long as we pass in 2 and 3. Pure functions are at the heart of functional programming, they are quite easy to test and debug and they don't affect anything on the global scope, we can stack pure functions on top of each other to create higher order functions.

const add = (a:number, b:number):number => a + b; // PURE FUNCTION

const square = (a:number):number => Math.pow(a, 2); // PURE FUNCTION

const addAndSquare = add(square(2), square(3)) 

console.log(addAndSquare) // 13tt
Enter fullscreen mode Exit fullscreen mode

We are just exploring the powers that using a functional programming style gives us, here we clearly demonstrate how we can pass functions as arguments to other functions. We can also take advantage of returning a function from another function. Functions that accepts functions as arguments or return a function are known as higher order functions.

const makeNumString = num => num2 => num + num2.toString();//
const num = makeNumString(2)
console.log(num(1)) // '3'
Enter fullscreen mode Exit fullscreen mode

Impure Functions And Side Effects

We have looked at the concept of pure functions, functional programming is built upon using pure functions in our code. Let's look at impure functions, impure functions are the exact opposite of pure functions. They are functions that cause Side Effects, impure functions and side effects are not totally bad and we use a lot of impure functions that cause many side effects in our codes daily. Side effects are results of impure function. A function that changes a variable that is not declared in it's scope is an impure function. While the result of that variable being changed due to the impure function is a side effect. In a more broad sense a side effect can be described as a change in the state of our application caused by an impure function. Let's look at another example of an impure function.

const state = { name: 'Job', likes: 'hardship' };
const impureFunc = (obj) => { = 'Dan';
    return Obj
const impureFunc2 = (obj) => {
    const obj.field = 'value'
    return obj
const impureFunc3 = obj => console.log(obj)
Enter fullscreen mode Exit fullscreen mode

All the example we gave above are all impure functions and like i said, there's nothing wrong with using impure functions in your code but you should pay extra attention to them and how they behave and the side effect they cause, the ones we gave above are impure because it;

  • Mutates a global variable that is outside it's scope.
  • Mutates a property on an object that is passed in as argument.
  • Changes the state of our application.

There are more categories by which we can classify as impure. So it's totally okay to use a functional approach to writing your code but try sticking to pure functions rather than impure functions but if you feel the need to use an impure function is justified then you should make sure that you understand the side effect of that function. That said let's use a functional approach to solving a very simple problem. Let's reverse an array and pick a random item from it.

// Reverse the array
const reverseArr = arr => arr.reverse() // Reverses an array

const getRandElement = arr => arr[Math.random() * arr.length]; // Picks a random element from an array

const arr = [1, 2, 4, 8, 9, 10, 21];

const randElement = getRandElement(reverseArr(arr))

console.log(randElement) // logs out a random element
Enter fullscreen mode Exit fullscreen mode

I hope you found this useful and interesting, feel free to leave a comment down below to share your knowledge and experience on functional programming.

Discussion (0)