Procedural vs Object-Oriented


C Version

    #include <stdio.h>

    /* Declare functions that will be used. */
    int populateData(int theData[]);
    int search(int theData[], int numItems, int searchValue);

    /** Start of program. */
    int main(int argc, char *argv[])
    {
        int theData[40];
        int numItems;
        int key = 42;
        int index;

        numItems = populateData(theData);
        index = search(theData, numItems, key);
        printf ("The index of %d in theData is %d.\n", key, index);
    }

    /** Populate the data in some way. */
    int populateData(int theData[])
    {
        /* code to populate theData instance variable goes here
             (e.g., might read it from a file) */
        return numDataItems;
    }

    /** Search for given value; return index. */
    int search(int theData[], int numItems, int searchValue)
    {
        int i;
        for ( i = 0; i < numItems; i++ )
        {
            if ( theData[i] == searchValue )
            {
                return i;
            }
        }
        return -1;
    }


Alyce Brady, Kalamazoo College