2

Suppose you have a function which just prints its arguments formatted:

__print_spec_result() {
  printf '%s  %s%s (%.3f s)%s\n' "$1" "$2" "$cyan" "$3" "$reset"
}

Here, $1 and $2 are arguments to the function, $cyan and $reset are constants.

Is this function pure in the functional sense?

Will Vousden
  • 32,488
  • 9
  • 84
  • 95
helpermethod
  • 59,493
  • 71
  • 188
  • 276

1 Answers1

3

No because printf is impure.

See from wiki:

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

And from another SO Question and his answer: Why is printf() an impure function?

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.

Community
  • 1
  • 1
Zelldon
  • 5,396
  • 3
  • 34
  • 46
  • 1
    Since this is bash where commands are used like `x=$(factorial 5)` then printing functions as return. `echo` is the identity "function" . – Sylwester Jun 29 '15 at 12:12