Functional Programming

Cosmic

Manners maketh man
Joined
Jul 4, 2009
Messages
12,888
Reaction score
951
FP$
29
Have you read much about, or used Functional programming?

One of the main ideas in Functional programming is to make functions more like mathematical functions, in that there can be no side effects when calling one. Several languages are beginning to implement functional programming, and probably most major languages have at least basic support.
 
ForumSource said:
Hm, aren't most functions mathematical?

I'll have to read more into this.
Nope. 🙂

Here's an example (C++):

Code:
int foo = 0;

function increment(int i) {
    foo += 1;
    return i + 1;
}
This function is NOT mathematical, because it has a side-effect. The variable foo is changed when the function is called. With a mathematical function, this is not possible.

For example:
Code:
f(x) = x + 1
That is essentially the same function as the C++ mentioned above, but since it's a mathematical function, the only effect which calling that function will have is giving you an output. It can't change anything else, like the variable "foo."

It's hard to say whether most functions are mathematical or not. Considering a function will generally do something other than return a value, such as write to the command line, or write to a file, most functions aren't really mathematical.
 
Back
Top Bottom