6D. Input, Output and Functions


Don't confuse reading and writing with parameters and returned values

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(double x)
  {
    return x*x;
  }
Students who become confused with input and output write
  double sqr(double x)
  {
    printf("What is x? ");
    scanf("%lf", &x);
    printf("The square of %lf is %lf\n", x, x*x);
  }
That 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 of the time, functions communicate with one another through parameters and returned values.