> As an aside, I
> personally think the distinction between function and procedure is
> nonsensical in languages like C/C++ & Java. Functions that don't return
> anything are just that - functions with a void return type. IMO the
> different names stem from different languages (eg pascal uses procedures),
> not from a real functional difference.
The different names comes from Math. Math defines a function as a
mapping between two sets, called the "domain" and "range". For every element
in its domain (the input), it must map to exactly one element in the range
(the output). Thus, technically, something which "returns" two values is not
considered a function (though it could return a single element, where that
element it itself a set or list or something with cardinality greater than
one; this is like returning a Vector<String> in Java -- you're not returning
multiple strings; you're returning a single vector of strings).
Functions, according to the mathematical definition, should have not
side effects. A function shouldn't "print to the console" or anything like
that; it should just, given an input, return an output.
Also note that functions shouldn't have state, nor return something
different given the same input.
"Sine" and "Cosine" are considered to be functions in Math.
A procedure, on the other hand, comes from the English term, meaning a
way of doing something. There is nothing implying that a value should be
returned or anything like that. "Place the key in the ignition, and turn
clockwise to start the engine" is a procedure, for example.
According to the Java Language Specification, Java doesn't have
"functions" or "procedures". They have "methods".
- Oliver
Stefan Ram - 03 Feb 2006 17:33 GMT
> Functions, according to the mathematical definition, should have not
>side effects. A function shouldn't "print to the console" or anything like
>that; it should just, given an input, return an output.
Even in programming languages, functions do not have effects.
Only the /evaluation/ of a function application might have
an effect.
This also happens in mathematics: For example, the evaluation
of the function "sin( 0 )" by a person might have the side effect
that he misses his train, because the evaluation takes time.
The difference is that in mathematics the effect
of the evaluation is not /specified/ by the function. While the
evaluation of "sin( 0 )" might have the effect that someone
misses a train, this is not specified for "sin" to happen.
In programming, some effects of evaluations of an operation
invocation are specified for the operation.
>Also note that functions shouldn't have state, nor return
>something different given the same input.
That is a true difference. However, in slightly complicated
way a call to a getter can be interpret as a mathematical
function, that returns a unique get-operation (always the
same), which then uniquely maps a state to a value. With the
additional convention that this value is considered as the
value of the original expression - which is where it
deviates from mathematical conventions. (This is the essence
of the use an meaning of monads in functional languages.)