7F.2. Parameters Control Information Flow


Implicit information flow

Functions need to communicate information among one another. 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 points (a, b) and (c, d), the beginner would write

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

For reasons discussed below, using global variables is not allowed by the standards for this course.


Difficulties with implicit information flow

There are serious difficulties with that. An obvious one is that it takes 5 lines to compute a distance. Another is that it encourages the programmer to use the same global variables to communicate with more than one function, and that can lead to subtle errors where one variable is being used for two purposes at the same time. (What if f  calls g and g changes the variables that f  thinks contain its information?) Using global variables hides information flow from view.

The next two subsections briefly explain two additional reasons, recursion and threads, why parameter passing is preferred to using global variables. If you don't care, skip ahead to the subsection on standards.


Recursion (optional)

Later, we will see a method for expressing algorithms called recursion. Some algorithms are easy to express (and code) using recursion but are complicated to express without recursion. Recursion makes your job so much easier that you can get some programs working in a reasonable amount of your time, but you will run out of time trying to fix errors if you do not employ recursion.

Recursion relies on a function calling itself, and global variables are toxic when a function calls itself.


Threads (optional)

Due to physical difficulties in getting individual processors to run faster, processor designers have instead concentrated on providing multiple processors or processor cores. The idea is for software to split itself into multiple threads of control that run simultaneously and can be performed by different cores.

That sounds great, but writing multi-threaded programs that use global variables if frought with difficulties. What if two threads of control try to change the same variable at the same time, each thinking that it has exclusive use of that global variable?

Multi-threaded software is the way of the future. Writing multi-thread software requires you to severely limit the use of global variables, and instead to rely on parameter passing and values returned by functions.


Standards

The standards for this course require all information to be passed through parameters and returned values. Do not use global variables to accomplish implicit implicit information flow.

Global constants are allowed. They represent fixed information that several functions can share. But make sure that you do not write a particular constant definition in more than place.