6.7. Functions

Do not change call-by-value parameters [CHANGE-CBV-PARAM: 1-5 points]

A function must not change the value of a parameter that is passed to it by value. See changing call-by-value parameters.

A function with a nonvoid return type must return a value. [NO-RETURN: 1-5 points]

If a function whose return type is not void can reach its end without returning a result, you will get a warning (assuming you have requested warnings). Pay heed to those warnings.

Only one loop per function [MULTIPLE-LOOPS: 2-8 points]

Each function body can only have one loop.

No duplicated function calls with same parameter [DUP-CALL: 2-6 points]

A function body must not make two calls to the same nondestructive function with the same argument when avoiding that would be a simple matter of remembering the result. For example,
  for(i = 0; i < strlen(s); i++) 
  {
    ...
  }
recomputes strlen(s) each time around the loop. If s is not changed in the loop body, then that duplicate call can be avoided as follows.
  int slen = strlen(s);
  for(i = 0; i < slen; i++) 
  {
    ...
  }

Do not write a function call a second time as a way to get the result from a prior call. For example,

  process(x);
  if(process(x)) 
  {
    ...
  }
computes process(x) twice, ignoring the result the first time and testing the result the second time. If your intent is to compute process(x) once, write
  if(process(x)) 
  {
    ...
  }

This requirement does not mean that a program can never call the same function more than once with the same parameter. It only applies to the body of a single function, and only for cases where avoiding the duplicate function call is a simple matter of remembering its result or avoiding wasted calls.


No extraneous parameters [EXTRA-PARAM: 1-3 points]

A function must not have extra parameters that either are unused or are unnecessary. A parameter is unnecessary if it provides information that the function could easily get from other parameters, or if the parameter is used solely as a local variable in the function, and neither passes information from the caller to the function nor passes information from the function to the caller.

Do not explicitly return a void value [RETURN-VOID: 1 point]

If expression E has a void type, do not write
  return E;

No extraneous returns [EXTRA-RETURN: 1 point]

Do not write statement
  return;
where it is not justified. For example,
  void demo(int& x)
  {
    if(x > 0)
    {
       x = 2*x;
       return;
    }
    else
    {
       x = -x;
       return;
    }
  }
has extraneous returns. It is equivalent to
  void demo(int& x)
  {
    if(x > 0)
    {
       x = 2*x;
    }
    else
    {
       x = -x;
    }
  }

No crippled functions [CRIPPLED: 1-4 points]

A crippled function is one that unnecessarily fails to do its entire job, and relies on its caller either to get it started or to finish the job. Examples of things that a crippled function might do are
  • insist that its caller initialize an array for it;
  • insist that its caller perform a final step for it;
  • not work correctly on all sensible parameter values, and insist that its caller work around that.

Do not treat a function that is not crippled as if it is crippled. [TRUST: 1-3 points]

If a function does a job, let it do its job. Do not try to work around a perceived mistake in the function that is not really there. For example,
  if(!isEmpty(x))
  {
    process(x);
  }
assumes that you either do not want to do process(x) if x is empty or that function process does not know that it should do nothing when x is empty. It that is a justified requirement of process (stated in the contract of process) then this is fine. But if process already does nothing on an empty value x, then just write
  process(x);

No strange compound jobs [COMPOUND-JOB: 1-2 pts]

Make each function have a sensible job. Do not make it do too much. For example, if your intent is to
  1. compute graph g;
  2. print a heading;
  3. print graph g
do not write one function that does the first two steps (compute graph g and print a heading). Those two jobs do not go together.

Do not unnecessarily tie jobs together [TIE-TOGETHER: 1-2 points]

This is really a continuation of the previous item. Where sensible, do not write a function that does two jobs so that, if you want to do one of the jobs, you are required to do both. For example, if you want to write the diameter of a graph, it makes sense to break the problem down into two steps: (1) compute the diameter of a graph; and (2) write the result. If you tie the two jobs together, so that the program cannot do one without doing the other, then the program is difficult to modify. Later, you might find that you want to compute the diameter of a graph without writing anything out. Remember that programs are frequently modified.