5F. Standards for Function Definitions

  1. One of the goals of this course is to show you how to break up a program into short functions. To that end, every function must have no more than 16 noncomment lines, where a noncomment line is defined to be any line that has at least 2 noncomment, non-white-space characters.

    Do not violate other requirements in other to reduce the number of lines in a function. For example, do not put more than one assignment statement on a line, as in

      x++; y++;
    

  2. Each function that returns a value (that is, its return-type is not void) must always return a value, unless it aborts the program.

  3. The parameters that we have seen so far are call-by-value parameters. A function body not change the value of any call-by-value parameter. For example, do not write

      int f(int n)
      {
        …
        n++;
        …
      }
    

  4. You do not need to store a value into a variable in order to return it. Do not do that without any justification. Instead of

      int result = g(y);
      return result;
    
    write
      return g(y);
    
    Instead of
      return result = g(y);
    
    write
      return g(y);
    

  5. Do not call a function with the same parameters more than once in the body of a function when

    1. the result would be the same for each call, and
    2. it is easy to avoid the duplicated calls by storing the result in a variable.

  6. C++ supports default parameters. We will not talk about them and you must not use them.

  7. C++ supports more things than we will use. We will not talk about instance methods (methods that belong to a class) or static functions. Do not use them.

  8. C++ allows gotos, which cause a function to jump to a labeled line. We will not talk about them and you must not use them.

  9. C++ supports exception handling (try blocks). We will not talk about exception handling and you must not use it.

See the standards for more details.