I admit that functional programming is one of the fields where I lack any deeper understanding, so excuse my ignorance for a while, but does that mean that any C function declared as void is a pure function as long as its name contains everything it does?
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.
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:)
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<typenameT>voidprocessThenOutputConcatenatedInputVariables(Tvar1,Tvar2){process(var1,var2);// side effect covered by the signaturestd::cout<<var1<<var2;}
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...
Hey! I'm YCMJason, a Software Engineer in London 👨💻. Love diving into tech puzzles and sharing them! 🧩
All views expressed here are my own opinions, so please take them with a pinch of salt! 🧂
Dev from EU, currently working in frontend development, with knowledge of docker and kubernetes.
I love tinkering and staying up with the latest technology.
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.
Dev from EU, currently working in frontend development, with knowledge of docker and kubernetes.
I love tinkering and staying up with the latest technology.
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:
has no effect on the world except returning something.
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.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I admit that functional programming is one of the fields where I lack any deeper understanding, so excuse my ignorance for a while, but does that mean that any C function declared as
void
is a pure function as long as its name contains everything it does?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.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:)
I thought that this can be solved by changing the signature?
Example:
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...Why not?
@tuxOr
There is nothing to do with the signature of a function.
A function is pure if and only if
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.I think I understood now, thank you!
Purity simply means:
If a function returns void and has no side-effects, it's pure, but it also doesn't do anything interesting.
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.Wouldn't that make any function in strongly typed languages "pure"?
No.
For example:
This function is not pure as it modifies
a
.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:
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.