views:

121

answers:

4

I'm writing a toy compiler thingy which can optimise function calls if the result depends only on the values of the arguments. So functions like xor and concatenate depend only on their inputs, calling them with the same input always gives the same output. But functions like time and rand depend on "hidden" program state, and calling them with the same input may give different output. I'm just trying to figure out what the adjective is that distinguishes these two types of function, like "isomorphic" or "re-entrant" or something. Can someone tell me the word I'm looking for?

+7  A: 

The term you are looking for is Pure

JaredPar
Thank god for that: I've never heard of it :-) I was sure I was going to kick myself when I heard the answer, but no, it's completely new to me. Thanks all!
Matthew Exon
+6  A: 

I think it's called Pure Function:

In computer programming, a function may be described as pure if both these statements about the function hold:

  • The function always evaluates the same result value given the same argument value(s). The function result value cannot depend on any hidden information or state that may change as program execution proceeds or between different executions of the program, nor can it depend on any external input from I/O devices.
  • Evaluation of the result does not cause any semantically observable side effect or output, such as mutation of mutable objects or output to I/O devices.

The result value need not depend on all (or any) of the argument values. However, it must depend on nothing other than the argument values.

Anton Gogolev
+1  A: 

I guess you could say the adjective is "pure" if you go by "pure function".

I always learnt that a function whose output is always the same when the arguments are always the same is called "deterministic". Personally, I feel that that is a more descriptive term. I guess a "pure function" is by definition deterministic, and it seems a pure function is also required to not have any side-effects. I assume that that need not be the case for all deterministic functions (as long as the return value is always the same for the same arguments).

Wikipedia link: http://en.wikipedia.org/wiki/Deterministic_algorithm

Quote:

Given a particular input, it will always produce the same output, and the underlying machine will always pass through the same sequence of states.

Roland Bouman
Deterministic has a different meaning. It means a function that takes a known amount of time to execute. The amount or time need not be the same every time but should be the same if the input is the same.
slebetman
slebetman: thanks! Do you have pointer for me so I can learn more about this? The wiki article mentions that in order to be deterministic, it has to pass through the same set of states, in the same order (and will thus lead to the same result), but doesn't mention it has to take the same amount of time in doing so. Or perhaps that's what you meant?
Roland Bouman