8

As far as I know, impure functions are those which do not always return the same value when called with the same parameters (I must be missing something, or may be wrong, correct me if I am).

So why is printf() considered to an impure function?

Richard JP Le Guen
  • 28,364
  • 7
  • 89
  • 119
cirronimbo
  • 909
  • 9
  • 19

6 Answers6

19

A "pure" function lacks side-effects too.

In other words, no matter how many times you call it, a pure function will affect nothing other than its output.

For example, foo is impure, even though it return zero:

int x;
int foo() { x++; return 0; }
int bar() { return x; }

If foo were pure, calling it would not affect the result of bar().

printf is impure because its result has "side effects" -- specifically, it prints something on the screen (or in a file, etc).
If it were pure, then you could call it a billion times and be sure nothing bad would happen.
But if you actually call printf a million times, there certainly is a difference to the user -- it fills up his screen (or disk space, or whatever). So clearly it's not pure.

Furthermore: If your output was redirected to be your own input (somewhat useless, but still), then calling printf would affect what you would receive from getchar. :) So it's directly observable that way, too.

user541686
  • 205,094
  • 128
  • 528
  • 886
  • 3
    While correct, this answer has nothing to do with `printf`. Nor does it address **WHY** `printf` is impure (external - as opposed to internal - side-effects). – David Titarenco Aug 12 '12 at 05:20
  • Is all functions impure. They time to execute (philosophy)! BTW - `bar` returns x – Ed Heal Aug 12 '12 at 05:21
  • 1
    @Mehrdad: I checked out on wiki. It **is** considered as **impure**. Thanks anyway for enlightening me. Before your post I surely would've considered this _foo_ as a **pure** function. – cirronimbo Aug 12 '12 at 05:23
  • @DavidTitarenco: I edited the question from "Is printf () an impure" to " why printf...". So he answered in context basically :) – cirronimbo Aug 12 '12 at 05:26
  • I have one more doubt: Is there any difference between being **impure** and being **referentially opaque** ? – cirronimbo Aug 12 '12 at 05:34
  • 1
    @cirronimbo: http://stackoverflow.com/questions/4865616/purity-vs-referential-transparency – user541686 Aug 12 '12 at 05:41
  • @cirronimbo "Before your post I surely would've considered this foo as a pure function" -- but not if you had understood *why* functions are declared "pure". For that, see my answer. As for "referentially opaque", it refers to a linguistic concept that isn't really properly transferable to programming. – Jim Balter Aug 12 '12 at 06:18
8

There are two parts to being a pure function. The first, as you stated, is that the function must consistently return the same value for the same input parameters. The second criterion, which printf does not fulfill, is that the function must not have side effects like I/O or object mutation.

verdesmarald
  • 11,646
  • 2
  • 44
  • 60
  • 2
    `printf` does not fulfill even the first criteria: it returns a number of characters **successfully** printed. – Vladimir Aug 12 '12 at 07:12
  • @Vladimir: Whenever you give the same input parameter, it will always return the same value ( which is the number of characters **successfully** printed ). So how does it **not** fulfill the first criteria ? – cirronimbo Aug 12 '12 at 08:32
  • 2
    The returned value may vary depending on a) implementation of printf and b) errors occured during writing. Imagine if stdout is closed. Here is an example: http://pastebin.com/HAtCm2Jh – Vladimir Aug 12 '12 at 08:57
  • @Vladimir Good point, I forgot about the return values in C/C++. I've been spending too much time writing Java where the return value of `PrintStream.printf` is `this`. – verdesmarald Aug 12 '12 at 14:43
3

Simply put, printf is impure because it does I/O. I/O by definition is impure because of the presence of external state of the I/O device (state which may vary from execution to execution).

David Titarenco
  • 32,662
  • 13
  • 66
  • 111
3

A lot of the answers explain that printf has I/O as a side-effect, but printf can have other side-effects too. For example, the %n specifier allows printf to write to a specified address (and is the cause of some security vulnerabilities).

jamesdlin
  • 81,374
  • 13
  • 159
  • 204
2

The significance of pure functions in programming is that the implementation can optimize out a call of a pure function if it already has the result of a call of that function with the same parameters. Clearly that cannot be done for calls to printf.

P.S. Even by your definition, printf is impure because it can return one value when it succeeds and a different value if there's an I/O error, e.g., out of space on the output device.

Jim Balter
  • 16,163
  • 3
  • 43
  • 66
0

printf() is impure because it causes output to an I/O device as a side effect.....

Akash KC
  • 16,057
  • 6
  • 39
  • 59