DEV Community

Discussion on: Functional programming basics part 1: Pure function

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
 
tux0r profile image
tux0r

that particular function (...) will most likely be made to do side effects stuff

I thought that this can be solved by changing the signature?

Example:

template <typename T>
void processThenOutputConcatenatedInputVariables(T var1, T var2) {
    process(var1, var2); // side effect covered by the signature
    std::cout << var1 << var2;
}
Thread Thread
 
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...

Thread Thread
 
tux0r profile image
tux0r

Unfortunately the signature wont make this function pure.

Why not?

Thread Thread
 
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.

Thread Thread
 
tux0r profile image
tux0r

I think I understood now, thank you!