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
2
u/vallyscode Nov 20 '23
How'd you define the boundaries between "declarative functional programing" and "imperative procedural programming"? I kind of got stuck with that question.
When looking closer, both approaches use functions and depending on language those can have guaranties of purity (like Haskell) or no (like Scala or JS and friends), functions are first class citizens and can be passed in the same way as data (higher order functions), ATDs are used frequently. And then I realize that C language also supports all that set of things, one can write function to be pure, can pass function to another function by reference, can even import function from dynamic library and call it, can compose multiple functions, so what's the conclusion we can make here, is code written in C still procedural and imperative or it is functional?