Back to Basics - Pure Functions
Tags: beginners, javascript, womenwhocode
Published: 2019-10-06 01:23:39

---
title: Back to Basics - Pure Functions
published: true
description: reviewing the fundamentals of pure functions
tags: #beginners, #javascript, #womenwhocode
cover_image: https://fitamalta.eu/wp-content/uploads/2019/03/what-is-coding.jpg
---

Pure functions are one of several core concepts of fundamental programming.  I'm sure you've written them, but were you able to identify that you were writing a pure function? Did you realize the importance or key traits of a pure function? If you're not sure then you've come to the right place. Let's review the basics of pure functions together! 

![chris pratt gif looking excited](https://media.giphy.com/media/rVbAzUUSUC6dO/giphy.gif)<figcaption>so excited</figcaption>

#### What are Pure Functions?

Simply put, pure functions are functions which accept argument(s), compute a value based on the argument(s), and return a value. They <em>always</em> do this.  Sounds really basic - right? Well, pure functions kind of are! But let's dig a little deeper into some additional key characteristics. 

Pure functions must always accept at least 1 argument.  This argument will remain unaltered but will be used in the computation which takes place inside the body of the function. Additionally, there must always be a return value. This is perhaps a dead giveaway of a pure function - the return value will always be the same when called with the same arguments. 

Take the code below - run `plusFive` 1 million times and you will always get the same return value when passed the same argument.  

![basic pure function example](https://thepracticaldev.s3.amazonaws.com/i/bz0fitzf64nbmcy2t6ok.png)<figcaption>very basic example, I'm sure you've used something like this before!</figcaption>


It's also important to note what pure functions <strong>don't do</strong>.  They shall not cause side effects such as altering the arguments that were passed in, changing your application's state, setting/updating global variables, or any other side effect like triggering a fetch request. They're simple and only depend on their arguments to compute and return a value.

#### Pure Functions in Your Program

Cool - now we've got the definition down let's take a look at how they might be used and some advantages.

Because pure functions are independent of your program's ecosystem, that makes them easier to debug and refactor.  You can expect the same output given the same input - so expect to write simple tests for these functions and if problems do arise you'll be able to quickly diagnose the issue. You can also most likely dismiss them as a culprit of other bugs knowing that these pure functions don't cause side effects. Another plus of no side effects is that when you refactor your pure functions you can kiss away any anxiety you might have about accidentally tweaking another corner of your program. Refactoring these functions won't change others in your program since they were never connected to begin with.


#### Examples of Pure and Impure Functions

First up - let's look at an <em>impure</em> function. Notice how the `moveUpGrade` function alters the argument that was passed in? That means the function is creating a side effect therefore it is impure. 

![impure function example](https://thepracticaldev.s3.amazonaws.com/i/see8bca1mwi1qxe31qhm.png)<figcaption>impure function</figcaption>

Let's refactor the code to make the function pure.  Below you can see we use the spread operator to create a new student object.  Instead of altering the exact object that was passed in and causing a side effect, we make the update desired and return a new object. Pure!

![pure function example](https://thepracticaldev.s3.amazonaws.com/i/ykbts4lndvm1d4mwlimu.png)<figcaption>pure function</figcaption>


And that's it for Back to Basics - Pure Functions! They're quite straight forward, can help maintain clean code, encourage simple refactoring, as well as quick testing/debugging. 

![that's all folks gif](https://media.giphy.com/media/xUPOqo6E1XvWXwlCyQ/giphy.gif)