7F.4. Inter-Function Communication

Some students confuse reading from stdin and writing to stdout with parameter passing and returning values from functions. Here is a function that computes the square of a real number.

  double sqr(const double x)
  {
    return x*x;
  }
Students who become confused with input and output write
  double sqr(const double x)
  {
    printf("What is x? ");
    scanf("%lf", &x);
    printf("The square of %lf is %lf\n", x, x*x);
  }
Mostly, students do that because they have been taught to do it. But it makes no sense. The parameter, x, represents information passed from sqr's caller to sqr. Don't try to get that information from some other source. The result is passed back to the caller, not written for the user to look at.

In a large piece of software, only a small percentage of functions do input or output. Most functions communicate with one another through parameters and returned values.