1

I am a C++ beginning learner. Recently, I read a paragraph describing the evaluation of an expression. The original text is as below:

"...Evaluation of an expression may generate side-effects, e.g. std::printf("%d", 4) prints the character '4' on the standard output...."

My question is "Why the character '4' caused by std::printf("%d", 4) is a side-effect?"

Can anyone give me a more comprehensive explanation or more examples about side-effects evaluated by expressions?

Thanks!

livemyaerodream
  • 890
  • 6
  • 19
  • 4
    "side-effect" is the word used for any effect of an expression other than the value being "returned" by it . E.g. generating output is a side-effect. You can read more about it in the C++ standard. – M.M Nov 03 '19 at 11:26
  • 1
    Not sure if it could be called a duplicate, but maybe [this question](https://stackoverflow.com/questions/9563600/what-exactly-is-a-side-effect-in-c) will help. – Lukas-T Nov 03 '19 at 11:26
  • What's the rest of the statement? I think by side effect they mean printf returns the number of characters outputted by it and this gets discarded due to the ; right after the printf statement. – srt1104 Nov 03 '19 at 11:28
  • 1
    @lucieon No, the returned value is the primary effect of the expression, not a side effect. Expressions have values, and (from a formal perspective) determining that value is primary. Any other effects are secondary. It seems odd from a functional perspective, but the primary effect of a `printf` *expression* is calculating the number of characters to print. Actually printing those characters is a side-effect. – JaMiT Nov 03 '19 at 12:18

1 Answers1

1

A side effect is any change in the system that is observable to the outside world.

Printing a number is clearly a visible change (also, internally you affect stdout state etc...)


Another important notion that can be helpful is the notion of pure function. It has two main characteristics:

  • A pure function is deterministic. This means, that given the same input, the function will always return the same output. ...

  • A pure function will not cause side effects. A side effect is any change in the system that is observable to the outside world.

Typical examples of functions violating these properties are:

static int n=0;
int foo_1(int m)   // not deterministic, without side effect
{
  return m+n;
}
int foo_2(int m)   // with side effect, but deterministic
{
  ++n;
  return m;
}
int foo_3(int m)   // with side effect, not deterministic
{
  ++n;
  return m+n;
}
int foo_4(int m)   // without side effect + deterministic = pure function
{
  return 2*m;
}
Picaud Vincent
  • 10,518
  • 5
  • 31
  • 70
  • I slightly disagree with your definition of side-effect since there are some side-effects that _aren't_ observable to the outside world; e.g., relying on RNG or making an assignment. – erip Nov 03 '19 at 13:35
  • @erip triggering a rng affects its state that is theoretically observable once you have used it. I tried my best to give a comprehensible answer. – Picaud Vincent Nov 03 '19 at 13:39