CSCI 2610 Lecture 6 Notes

Function contracts

A function contract describes what the function does. A function contract should have the following characteristics.
  1. A contract should be precise. Avoid vague contracts.
  2. A contract should be written for a function before the function is written.
  3. A contract should always discuss the significance of every parameter.
  4. A contract should never discuss the local variables of the function body, except when those names are only used for convenience in describing what the function does.
  5. A contract is not a road map to the function body. (It can't be, since it must be written before the function body is written.)

WARNING. You will have a tendency to try to write your functions without writing contracts first. This is very bad practice, and will cause you to spend more time writing your programs than if you write the contracts first. The contracts serve as guides to your work, and are critical aspects of the programming process. If you skip over them, you can count on difficulties.

Examples

A function to compute the smaller of two numbers

    /////////////////////////////////////////
    //              min                    //
    /////////////////////////////////////////
    // min(x,y) returns the smaller of x   //
    // and y.				   //
    /////////////////////////////////////////

    int min(int x, int y)
    {
      if(x < y) return x;
      else return y;
    }

A function that tells whether a number is prime

    /////////////////////////////////////////////
    //                prime                    //
    /////////////////////////////////////////////
    // prime(n) returns 1 if n is prime, and   //
    // 0 if n is not prime.                    //
    /////////////////////////////////////////////

    bool prime(int n)
    {
      if(n <= 1) return 0;
      else {
        int k = 2;
        while(k < n) {
          if(n % k == 0) return 0;
          k++;
        }
        return 1;
      }
    }

A function that computes the next prime after a given number

    ///////////////////////////////////////////////
    //                nextPrime                  //
    ///////////////////////////////////////////////
    // nextPrime(n) returns the smallest prime   //
    // number that is greater than n.            //
    ///////////////////////////////////////////////

    int nextPrime(int n)
    {
      int k = n+1;
      while(prime(k) == 0) k++;
      return k;
    }

A function that prints the first n prime numbers

    ///////////////////////////////////////////////
    //                PrintPrimes                //
    ///////////////////////////////////////////////
    // PrintPrimes(n) prints the first n prime   //
    // numbers, one per line.                    //
    ///////////////////////////////////////////////

    void PrintPrimes(int n)
    {
      int k,i;
      k = 2;
      i = 0;
      while(i < n) {
        cout << k << endl;
        i++;
        k = nextPrime(k);
      }
    }

A main program that prints the first few primes

    ///////////////////////////////////////////////
    //                main                       //
    ///////////////////////////////////////////////
    // Read a number n and print the first n     //
    // primes.                                   //
    ///////////////////////////////////////////////

    int main()
    {
      int n;
      cout << "How many primes shall I print? ";
      cin >> n;
      cout << "The first " << n << " prime numbers are:\n";
      PrintPrimes(n);
      return 0;
    }