I'm reading about pure-functions in functional programming and am wondering, whether a function being deterministic implies that the function is also side-effect free? (and vice versa?)

  • 1
    i'm guessing by deterministic you mean referentially transparent? but there are at least a couple of other things it could mean (particulalry in FP non-deterministic is sometimes used to mean returns a list of possible outcomes) – jk. Mar 15 '17 at 12:01
  • Yes I'm pretty certain referential transparency is what I meant. By deterministic I mean for example sin() (always returning the same output when given a particular input), so sin(90) can be substituted by 1 – user66875 Mar 15 '17 at 12:14

Pure = deterministic + without side effects

A function is pure only, if both criteria are met. If it meets only one of them, it's not pure.

Deterministic but with sideeffects:

As pointed out by @Caleth

int DeterministicButSideeffects(int param)
    Console.Writeline("Sideeffect"); // Side effect here
    this.someVariable = param; // Another side effect

    return param; // Result only depends on the parameters

Without sideeffects but not deterministic

int NonDeterministicWithoutSideeffects(int param)
    return param + getRandomIntNumber(); // Result depends on random number

Note that side effects are only "outbound". If a function modifies the state of the containing code (global variable or field in a class) or if it performs some I/O-operations, it has side effects.

Another very simple function that is not deterministic would be:

DateTime GetCurrentDateTime()
    return DateTime.Now; // -> Result depends on current datetime


int add(int num1, int num2)
    return num1 + num2;
  • 4
    arguably getting a random number does perform a side effect as it either does some IO (get some hardware measurement ) or modifies some global state (internal state of the prng) – jk. Mar 15 '17 at 14:36
  • @jk: It could read uninitialized memory which is likely not I/O proper. Even if nothing else changes the contents of the uninitialized memory, the function will return the same result in further invocations, but the result will be unpredictable. It's still a dependence on an external process, that is, the physical process that flips bits of powered-off memory. – 9000 Mar 15 '17 at 16:17
  • 1
    reading uninitialized is a spectacularly bad way to implement a prng. un-initialized memory is not guaranteed to be in anyway random – jk. Mar 15 '17 at 16:21
  • @jk correct. depending on the implementation of getRandomIntNumber(), side effects could happen (like changing state of the prng). – Stefan Woehrer Mar 15 '17 at 19:22
  • @9000 stackoverflow.com/a/31746063/207716 gives good reasons why you can't write a PRNG by just reading unitialized memory – jk. Mar 16 '17 at 10:59

It's easy to show that a function being deterministic doesn't imply that it is pure, with a simple counterexample:

int DeterministicButNotPure(int param)
    Console.Writeline("Foo invoked"); // Side effect here
    return param; // Result only depends on the parameters
  • Ok, thank you. But All pure functions are deterministic, right? – user66875 Mar 15 '17 at 11:08
  • This site is about conceptual questions and answers are expected to explain things. Throwing code dumps instead of explanation is like copying code from IDE to whiteboard: it may look familiar and even sometimes be understandable, but it feels weird... just weird. Whiteboard doesn't have compiler – gnat Mar 15 '17 at 11:21
  • 4
    @gnat this is a conceptual answer: it shows that the implication "function is deterministic" if and only if "function is pure" doesn't hold, by providing a counterexample: a simple function that is deterministic, but isn't pure – Caleth Mar 15 '17 at 11:27
  • I think this sort of answer is helpful. However, it only answers one question but not if the opposite direction (Purity implies determinism) always holds. – netik Mar 15 '17 at 11:48
  • @netik the definition of a pure function implies that. "A pure function is pure when it is deterministic and doesn't have side effects." – Pieter B Mar 15 '17 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.