5C. What Are Parameters and
What Are They Good For?


Where do parameters come from?

Some beginners are uncomfortable with parameters. Look at this function definition.

  // sqr(x) returns the square of x.

  int sqr(int x)
  {
    return x*x;
  }
A reasonable question is, what is x? How can I multiply x times itself if I don't know what x is?

The answer is that the responsibility for choosing a value for x lies with the function call, not with the function definition. Statement

  int w = sqr(20);
chooses value 20 for x.

It is crucial that you learn to compartmentalize, to think only about a small piece of a computer program at a time. When writing a function definition, don't think about where its parameters will come from. That will be handled someplace else.


Controlling information flow

Functions need to communicate information among one another. For example, the distance function needs to know which two points to use.

Sometimes beginners are taught to write functions that communicate through shared (global) variables. For example, instead of using parameters, a beginner might write the distance function as follows.

  double x1, y1, x2, y2;

  double distance()
  {
     return sqrt(pow(x1 - x2, 2), pow(y1 - y2, 2));
  }

Then, to compute the distance between (a,b) and (c,d), the beginner would write

  x1 = a;
  y1 = b;
  x2 = c;
  y2 = d;
  r = distance();

Beyond the awkwardness of using 5 lines to compute a distance, there are some serious difficulties with that.

  1. Information flow is a big deal is software. Information is constantly flowing, one way or another, from one function to another, and a programmer needs to have a good understanding of just what is happening in his or her software.

    Using global variables makes information flow implicit. It is hidden behind the scenes. That makes it difficult for a programmer to keep track of what is going on.

    Parameters and return values make information flow explicit. The program clearly states what information is being passed to a function. Use of a value returned by a function is also explicit in the program.

    The standards for this course require all information to be passes through parameters and returned values. Except in some special circumstances, you must not use global variables, which are any variables that are not created in parameter lists or function bodies, and that are shared by two or more functions. (C++ also allows something called a static variable that some students might have encountered. We will not talk about them. They are also forbidden by the course standards.)

    The cost in points for using global variables to achieve implicit communication of information among functions will be very high. Don't do it.

    Global constants are allowed. They represent fixed information that several functions can use. Don't duplicate a constant definition by defining it in each function that needs it.

  2. For a long time, the speed of computer processors steadily increased. Most of that improvement came from making transistors smaller. Smaller transistors can switch on or off more quickly than larger transistors.

    At a processor clock speed of around 3GHz, processor makers hit the heat barrier. Transistors create heat as electricity flows through them, and the faster they switch on and off, the more heat they produce. Even though processor makers did everything they could to reduce the amount of heat that each transistor produced, there came a point where the physics of transistors got in the way of further reductions.

    To get around that problem, instead of making each processor faster, they put more processors in computers, with the idea that a multi-threaded program could go faster by using several processors at once.

    Multi-threaded programming is growing in importance. But it requires even more careful control over information flow than traditional, single-threaded programming. Unless handled with great care, global variables are usually lethal to the correctness of multi-threaded programs, and a key to multi-threaded software is to do almost all communication through parameters and return values.


Do not confuse parameters and return values with input and output

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.