DEV Community

Cover image for Functional programming basics part 1: Pure function
ysael pepin
ysael pepin

Posted on

Functional programming basics part 1: Pure function

What is purity?

Purity for a function is defined as one that will always return the same output for the same input and perform no side effects.

So what do we mean by same output for the same input?

well... lets see some examples...

lets say we have this code:

let y = 2
const addYtoX = (x) => x + y
Enter fullscreen mode Exit fullscreen mode

This function is not considered pure as the output could be modified by the value of y witch mean we cannot guaranty it will return the same output for the same input.

let y = 2;
const addYtoX = (x) => x + y

console.log(addYtoX(3)) // 5

y = 1 
console.log(addYtoX(3)) // 4

Enter fullscreen mode Exit fullscreen mode

As you can see the value of y will affect the output of the function even if we call it using the same input... this is definitely impure.

Lets look at the pure version of the same function:


const addYtoX = (x, y) => x + y
Enter fullscreen mode Exit fullscreen mode

There you go!!!

I can call this guy as many times as I want and I can be sure to get the same output for the same input.

That's cool man, but what about that side effect thing?

well... lets see the same function but with added side effects!


const addYtoX = (x, y) => {
    makeSomeNoodles()
    return x + y
};
Enter fullscreen mode Exit fullscreen mode

I hope you can guess the side effect is caused by the makeSomeNoodles function call right?

Why is that bad? Well... the function is suppose to add x to y and now it is making noodles before computing my values? And I have no idea what is happening during that function.... maybe it is actually crashing my simple function before it can execute or worst!!!

Anyway, I hope you get the basic idea. :)

Until next time... stay pure!

Top comments (20)

Collapse
 
pizmovc profile image
Luka

I don't think that the void return type has anything to to with function purity. A function can be pure even if it returns some value. It just shouldn't modify anything outside of it and does not use any variables that are defined outside of its scope. Its return value should be determined solely by its input parameters.

 
ycmjason profile image
YCM Jason

@tuxOr

There is nothing to do with the signature of a function.

A function is pure if and only if

  1. The returned value is calculated only with the arguments
  2. It does not change any thing outside of a function (side effect)

So in your example, assuming process(var1, var2) will make some side effect somewhere else in the program, it is not a pure function.

However, if process do not make any changes, then it will still be a pure function.

 
pizmovc profile image
Luka

No.

For example:

int a = 5;

int sumOfNumbers(int x, int y) {
  a = 1;
  return x + y;
}

int sum = sumOfNumbers(12, 33);

This function is not pure as it modifies a.

Collapse
 
ysael profile image
ysael pepin

Yes I would say that Luka is right.

And would add that for me, making a function that returns void is a way of declaring that that particular function is impure and will most likely be made to do side effects stuff.... cause we need to do them sometimes:)

Collapse
 
euler2718 profile image
John Corley

I think something that returns VOID is not even a function at all, but a procedure....don't functions need input/ouput by mathematical definitions?

Collapse
 
jreina profile image
Johnny Reina

This is the working definition that I use.

Collapse
 
ysael profile image
ysael pepin

that is a great way to describe it I think!

Collapse
 
theodesp profile image
Theofanis Despoudis

Currently this is not valid Javascript:

const addYtoX(x, y) => x + y

But this is:
const addYtoX = (x, y) => x + y

Collapse
 
ysael profile image
ysael pepin

oups! I just corrected that, thanks a lot!

Collapse
 
qm3ster profile image
Mihail Malo

Any useful function returning void is definitely impure.
Even if it doesn't reference anything besides its parameters, it's still bound to mutate or otherwise bother the parameters.
A pure function:

  1. has no effect on the world except returning something.
  2. the return value depends only on the parameters.

One of the consequences of 2. is that when given the same parameters, it should always produce the same return value.

This is a useful dipping stick that demonstrates that functions that depend on eg time or randomness aren't pure.

 
ysael profile image
ysael pepin

Unfortunately the signature wont make this function pure.

What you would need in this case is composition which would allow you to compose 2 functions. I will at one point cover composition, in the mean time, you can read that great article by Eric Elliott --> medium.com/javascript-scene/master...

Collapse
 
stojakovic99 profile image
Nikola Stojaković • Edited

Luka is right. For function to be pure it shouldn't change things out of it's scope too which void function can do. It's because whole point of pure functions is to write more predictable code.

Collapse
 
kayis profile image
K

Funny that you choose JavaScript for the examples, while it doesn't support pure functions.

Collapse
 
ysael profile image
ysael pepin

There is a good article on the topic that seems to spark some interesting debates--> hackernoon.com/do-pure-functions-e...

I feel we can still benefit from the concept and that indeed JavaScript will remain a language full of surprises ;) Maybe that is why we like it so much in the end?