Lecture 10: Large Programs

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- Real programs are very large.
	- eg:  telephone switching networks =>  25 million lines

- Consequence:
	- we must divide these programs up into chunks so that
	  many programmers can work on them independently.

- even when programs are not 25 million lines, we need ways of organizing
our functions and data into related groups so our programs are easier
to create, understand, and debug

- a module is a "chunk" of a program, a group of related definitions or 
functions.

- modules can be programmed separately by different programmers and 
compiled independently
	
- we build up a full program by glueing together a bunch of compiled
modules (this is called linking)

- the question of how to break a program up into chunks is complicated.  
In general: 

	1) the components of a module should all be related.  They
		cooperate to complete some task.  They are somehow similar.

	2) the components of one module should depend upon the components
		of another module as little as possible. 
		
- eg: a train.  Each car in the train is like a module.  For the train
to function it needs all its cars.  Each car is linked to other cars
but the cars themselves form separate units:

	- the engine car =>  provides power, drives the train
	- the passenger car =>  place for passengers to sleep
	- the baggage car =>  storage for passenger belongings
	- the dining car =>  place for passenger to eat 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- a module contains 2 parts:

	- an interface:  a specification of what pieces of data, 
		operations the module provides and what these operations
		do.

	- an implementation: the actual code, data structures which define the
		operations specified in the interface.

- interface: a description of *what* will be done
- implementation: *how* you do it.

eg: module CS113: 
	interface:  m is a method which guarantees success in 113
	implementation:  m == spending 25 hours on assignment #2

eg: module string:
    interface:
	int strlen(char *s);
	int strcmp(char *s, char *t);
	char *strcpy(char *s, char *t);
 
    implementation:
	int strlen(char *s)
	{
		int i = 0;
		while(s[i] != '\0')
		  i++;
	
		return i;
	}

	int strcmp(char *s, char *t)
	{
		int i;
		for (i =0; t[i] != '\0'; i++)
			...
	}

	char *strcpy(char *s, char *t)
	{
		int len = strlen(t);
		...
	}

In C, we have modules and they have interfaces and implementations:

- C interfaces:
  - contain declarations:
	- function prototypes
	- #define's
	- variable declarations(but *not* initialization)
  - include other module *interfaces* if the interface depends upon 
	another module (#include < stdio.h> )
  - are put in .h files
  - eg: string.h

- C implementations:
  - contain definitions:
	- function definitions
	- variable declaration and initialization
  - include other module *interfaces* if the implementation depends upon 
	another module (#include < stdio.h> )
  - include the header file for this module
  - are put in .c files
  - eg: string.c

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- users of a module only need to know the interface to module to be
able to use the module.  They do NOT need the implementation.

eg:  module: stdio has functions printf and scanf.  
You have used these functions throughout the course without knowing what their
implementation is!  You only know the interface to this module - *what* 
printf and scanf do.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

- standard library modules vs user-defined modules:

/* Standard Libraries: 
   angle brackets - look in system folder for module */
#include < stdio.h> 
#include < string.h> 

/* User Defined Modules: 
   double quotes - look in current directory for module */
#include "mymodule1.h"
#include "mymodule2.h"

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~