CSCI 2610 Lecture Notes 3


Basics of functions

A function can be thought of as a box that is given some inputs and receives some outputs. The inputs are given to it not by the user sitting at the keyboard but by another part of the program. Inputs are called parameters. For example, the sqrt function takes in a number, and produces out the square root of that number.

You write a simple function like this.

    return-type name (parameters)
    {
      computational instructions
      return return-value;
    }
The return-type is the type of the result. For example, the return-type of the sqrt function is double. The name is the name of the function. Each parameter in the parameter list consists of a type and a name for the parameter, in that order. If there are two or more parameters, they are separated by commas.

For example, here is a function that takes in a real number (type double) and returns the square of that real number. If it is given 5.0, it returns 25.0.

    double sqr(double x)
    {
      return x*x;
    }

Here is a function that returns the maximum of two integers.

    int max(int a, int b)
    {
      int result;
      if(a > b) {
        result = a;
      }
      else {
        result = b;
      }
      return result;
    }

The maximum finding function can be written in a shorter form. Here it is.

    int max(int a, int b)
    {
      if(a > b) return a;
      else return b;
    }

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;
    }