3 ms·
That's about right. A "function" in functional programming is supposed to be just like the functions you remember from grade-school algebra class, e.g., f(x)
by profquail 15y ago
That's about right. A "function" in functional programming is supposed to be just like the functions you remember from grade-school algebra class, e.g.,
f(x) = 2x + 5
If you look at that function, you'll notice that for any given value of 'x', the function always produces the same output value. For example, no matter how many times you try it, plugging in 3 for 'x' always returns 11. This means that f(x) is a "pure" function.
The only difference between the trivial example with f(x) above, and say, Haskell, is that the functions you're working with in Haskell are much more complicated. Boil it down though, and they're still pure, mathematical functions nonetheless.
Now, you can (should?) write purely functional programs in C if you follow the same principals. The main difference is that C doesn't force you do write your code this way, and since writing purely functional code can be a bit tedious, most people don't.
This leads to impure functions in your code, like when you read a file from disk -- if the file is there, the function does one thing; if not, it may do something else (like crash). Since the output of an impure function can vary depending on when/where/why/how it's executed, it can be much harder to find bugs in it, since the code may compile and run just fine until the moment it doesn't.
Anyway, that's the basic rundown on FP. I'm not an FP expert, so if I've missed anything, someone please correct me.