I now have all the functions in a source file which I have called lcd.c (the .c is the language extension which is usually used for C program source files) Now I want to use the lcd functions in my test program. To call a function the C compiler needs to know what the function looks like, in terms of its name, the type of the function, and the name and type of all the parameters. It doesn't need the actual code of the function itself, just the header.

You can tell a compiler about a function by showing the compiler the prototype of a function. The prototype is just the header, with no function body. I have created a file containing prototypes for all the lcd functions:

/*  lcd display driver code  */
/*  header file  */

char lcd_start ( void ) ;

char LCD_clear ( void ) ;

char LCD_print_ch ( char ch ) ;

char LCD_print
    ( const char * message ) ;

char LCD_cursor
    ( char x, char y ) ; 

The function body is replaced by a semicolon. Once the compiler knows about these function prototypes it knows how to call the functions. C programmers call such a file a header file, and it is normally held in a file with the same name as the C source file, but with the language extension .h.

We can then use the #include directive we saw above to include the contents of the header file in our test program:

/*  LCD Test Program  */

/* fetch the headers     */
/* for the LCD functions */

#include "lcd.h"

void main ( void ) {
     LCD_start () ;
     LCD_clear () ;
     LCD_print ( "hello world" ) ;
     LCD_cursor ( 10, 2 ) ;
     LCD_print_ch ( 'X' ) ;
}     

This is not a proper test, in that it ignores any error replies (but where would it display the error messages?!) but it serves to show how one function can use the code in another.