<-- back to index...

Keunwoo Lee : CSE 142 : 14 October 1999

Computer languages, like natural languages, have both nouns and verbs. You have already learned about variables, which are the nouns of a computer language. What are the verbs?

    #define HASH_MULTIPLIER 11
    #define HASH_MODULUS    1000

    /* A very trivial function. */
    int add(int first, int second)
    {
        return first + second;
    }

    /* Basically slices and dices a number for no good reason. */
    int hash(int seed) 
    {
        int temp;
        temp = seed * HASH_MULTIPLIER;
        temp = temp % HASH_MODULUS;
        return temp;
    }

    /* This function calls another function. */
    void print_hash(int seed) 
    {
        int hashed = hash(seed);
        printf("%d", hashed);
    }

    /* main, Lord Of All Functions. */
    int main(void) 
    {
        int a, b, seed; /* Bad names, but for our purposes it's ok */

        /* Test the most trivial function, addition. */
        a =  282;
        b = 6343;
        printf("a + b returns: %d\n", a + b);
        printf("add(a,b) returns: %d\n", add(a,b));

        /* See what hash does. */
        seed = hash(b);
        printf("hash() returns: %d\n", seed);

        /* Try the last function. */
        print_hash(seed);

        return 0;
    }
    

How to call a function:

  1. Evaluate every comma-separated parameter.
  2. Put each parameter somewhere (magic).
  3. Hand control to the function.
  4. When the function hands back control, get the return value (if any).

How to be a function:

  1. Set up your local variables.
  2. Execute your statements one by one.
  3. On return, put your return value, if any, in the return space (magic).
  4. Make your local variables and parameters disappear. (magic)

Brief pedantry on software development

Two styles of student development:

  1. Big Bang Method:
    1. Write a mountain of code.
    2. At the end, press "compile" and hope that it works.
  2. Incremental Method:
    1. Write a little code (e.g., one function).
    2. Write a dummy main() program that just tests that code. Fix whatever's broken.
    3. Forget the now-working function. Start the next function, and do the same thing.
    4. Repeat until all functions are written.

Guess which one works better? Unfortunately, in practice, most students use a hybrid, compiling incrementally but testing only at the end, adding greatly to their suffering.


Keunwoo Lee
Last modified: Tue Nov 2 17:42:06 PST 1999