4B. Side Effects and Ignoring the Value of an Expression

Side-effects

Although some C++ functions work just like the functions that you encountered when studying algebra, some C++ functions have an additional characteristic: they perform actions, called side-effects.

For example, we will shortly see a function called printf that is used to write out strings. Expression printf("mouse") has the side-effect of writing string mouse (on the standard output, which is typically the console).


Ignoring the result of a function call

Some functions, like printf, do not produce a value at all; you only use them for their side-effects. When a function does not have a result, write a semicolon after the function call to turn the call from an expression into a statement. For example,
  printf("I really like apples\n");
writes I really like apples on the standard output, followed by a line break character (\n).

Type void

The return-type of a function can be void, indicating that the function does not return anything; it only has side-effects. For example, the return-type of printf is void.

You are required to ignore the result of a function with a void return type.

Usually, you do not write a return-statement in a function whose return-type is void. The function returns when it reaches its end. If you want to force a return, say

  return;

Indenting long function calls

A function call is not required to occupy a single line. If the call is long, break it into shorter lines. Indent all but the first line. It is common practice to indent to just after the left parenthesis in the function call. For example, write
  printf("The sun was shining on the sea,\n"
         "Shining with all his might:\n"
         "He did his very best to make\n"
         "The billows smooth and bright--\n"
         "And this was odd, because it was\n"
         "The middle of the night.\n");
(Recall that two or more string constants in a row are combined into a single string constant.)

Ignoring the value of expression

You can ignore the value of any expression by putting a semicolon after the expression, turning it into a statement. For example,
  double x = 4.0;
  sqrt(x);
declares variable x and initializes x to 4.0. Then it computes sqrt(x), which has value 2. But the 2 is ignored. Nothing is done with it. Variable x still has value 4.0.

It is only sensible to ignore the result of a function call that has a side-effect. Since the sqrt function does not have any side-effect, ignoring its value is silly. But you can do it.

>

You can ignore the value of any expression. For example,

  int n = 0;
  n+1;
declares integer variable n and initializes it to hold 0. Statement
  n+1;
computes the value of n+1 and does nothing with the result. It does not change n


Variable assignments are actually expressions

An assignment such as n = 1 is actually an expression whose value is 1 but that has the side effect of storing 1 into n. Symbol = is treated like a binary operator, such as +. Operator = is done from right to left, so
  int a,b;
  a = b = 1;
is the same as
  int a,b;
  a = (b = 1);
which stores 1 into both a and b.

The standards for this course require that you not use an assignment as an expression except in the context of a multiple assignment, as shown. For example, do not write

  double x = sqrt(z = 4.0);



Exercises

  1. What is a side-effect Answer