r/functionalprogramming • u/Ok_Wishbone_5018 • Nov 20 '23
Question Is the code still functional programming?
If i declare a variable inside a function and mutate it, is it still considered functional?
For example, if i write a function to find the max of an array with a for loop and a max value and return that max value the function is still pure. It doesn't have any side effects, since the max variable is not global but it does contraddict the principle of immutability of data. So my question is, can you use for loops and variables inside of functions or not?
(ik of another way of getting the result using functions like reduce in javascript, thats not the point of the question)
View all comments
Show parent comment
3
u/[deleted] Nov 20 '23
Well this can be considered as mutations already cause your functions anyhow have now context outside of their bodies, even if it's other private functions.
The thing is if you correctly structure your FP code there won't be questions like this. You're not trying to squeeze functionality into your code. Functional programming is happening naturally once you develop certain approach to data structures and how data flows.