Welcome to Part 7 of my C tutorial.
I think that now would be a good time to talk about functions.  Let's 
get into it right away shall we?
There are two types of functions in C.  Those that return a value and
those that don't.  Let's start by looking at functions in general.
Let's discuss the function called "main" which exists in all C programs.
In all of our programs so far, "main" has passed no parameters (that's
the part in the brackets...where the word void is) and ALWAYS returns
an "int".  The function "main" is the only function that actually has
rules regarding what can be passed or returned from it.
Let's now discuss how to go about making our own function:
First of all, there are three parts to a function.  The return type,
the function name and the parameter list.  Let's take a look at our
"main" function one more time:
int main(void)
There you have the three parts of a function.  The word "int" is the
return type.  Let's chat a little about that, shall we?  The return
type of a function can be any of the data types provided by the C
language.  Some examples are:  float, double, long, int and char.
Well that's all fine and dandy, but what if you don't need to return
something from one of your functions (Just like a procedure in Pascal).
If you have nothing to return to the calling program then you can use
word "void" as the return type.
Now let's continue on to the parameter list.  In our "main" function,
we have the word void.  You use the word "void" when you have nothing
to pass to the function.  Let's say you do have something to pass.
Let's take a look at how to pass parameters:
int cube(int x)
The above function passes an int and returns an int.  This is shown
much easier by writing a program for it.  So we will:
/*------------------------------------------------------------------
 Program:       CUBE.C
 Author:        CORVUS
 Date:          Aug. 28, 2001
 Description:   This is a program that gets the cube of
                a number.
------------------------------------------------------------------*/
#include <stdio.h> 
int cube(int c);   /* This is a prototype */
int main(void)
{
  int x = 10, y = 7;  
  int v;
  printf("X = %d\n",x);
  v = cube(x);        /* Call the cube function using x as argument */
  printf("Now X = %d\n\n",v);
  printf("Y = %d\n",y);
  v = cube(y);        /* Call the cube function using y as argument */
  printf("Now Y = %d\n\n",v);
  return 0;
}
int cube(int c)
{
  /* c is the parameter.  It get's the value
     of whatever was passed into here.  In first
     case above, c is given the value 10.  We
     then return (c*c*c).
  */
  return (c*c*c);
}
The output I recieved was:
X = 10
Now X = 1000
Y = 7
Now Y = 343
Let's start my explanation way up at the top of the program, with
the line stating: int cube(int);  /* This is a prototype */
Ok.  What is a prototype anyway?
The prototype tells the compiler about the return type, name and
parameter list of the "cube" function.  The compiler uses this
information to make sure you didn't make any errors while calling your
function.  If it finds something wrong, it will generate an error.  A
prototype should always look like the function header.  Just
cut-&-paste.
Let's move down a few lines inside "main".  We are going to look at the
way we called our function:
v = cube(x);  /* Call the cube function using x as an argument. */
What does this do?  Well, it calls the cube function passing the 
argument in the brackets.  In our case x was equal to 10.  Anyway,
we jump down into the function itself.  In there it multiplies x by
itself 3 times.  It does this all in the "return" statement.  Yes.  The
return statement can do math.  After it does the multiplication, it
jumps back to where it was called and places the new value in v.  It
then goes down to the next line and prints out the new value.  The same
thing happens a little farther down at the next function call.
That's all there is to it.  There is one last thing.  What if you don't
want to return anything?  Kinda like the procedure statement in Pascal.
All you have to do is define the function as "void".  Let's go over an
example program:
/*------------------------------------------------------------------
 Program:       VOIDFUNC.C
 Author:        CORVUS
 Date:          Aug. 28, 2001
 Description:   This program calls a function to display some
                information about the program.
------------------------------------------------------------------*/
#include <stdio.h> 
void DisplayHeading(void);  /* Prototype */
int main(void)  
{    
  printf("Hello.  I'm about to call my DisplayHeading() function\n\n");
  DisplayHeading();  /* Call function */
  printf("Now I'm back inside \"main\".\n");
  return 0;
}
void DisplayHeading(void)
{
  printf("Hello!  You are currently inside the DisplayHeading() ");
  printf("function.\n");
  printf("Now it's time to Go!\n\n");
}
So there you have it.  Now let's try to seperate the insert sort
algorithm into it's own function.  Here's the original program:
/*------------------------------------------------------------------
 Program:       INSORT.C
 Author:        CORVUS
 Date:          Sept. 17, 2001
 Description:   This is a program that sorts an array of 10 integers
                using the Insert sort algorithm.
------------------------------------------------------------------*/
#include <stdio.h>
int main(void)
{
  /* Initialize our array */
  int Num[10] = {234, 212, 0, 21, 14, 175, 998, 401, 1232,110};
  int i;      /* Loop counter for displaying results */
  int outer, inner;  /* Sorting variables */
  int tmp;
  /* Display the unsorted array
     on the screen. */
  for (i = 0; i < 10; i++)
    printf("%d\n", Num[i]);
  /* Do Insert Sort */
  for (outer = 1; outer < 10; outer++)     /* look for insertion point. */
    for (inner = outer; (inner > 0) && (Num[inner - 1] > Num[inner]); inner--) {
    /* Move the others down and insert it. */
      tmp = Num[inner];
      Num[inner] = Num[inner - 1];
      Num[inner - 1] = tmp;
    }
  /* print sorted array */
  printf("\n");
  printf("=====\n\n");
  for (i = 0; i < 10; i++)
    printf("%d\n", Num[i]);
  return 0;
}
Ok.  So there you have our familiar sorting program.  Let's change
things a little bit by focusing our attention on the insert sort area
itself:
  for (outer = 1; outer < 10; outer++)     /* look for insertion point. */
    for (inner = outer; (inner > 0) && (Num[inner - 1] > Num[inner]); inner--) {
    /* Move the others down and insert it. */
      tmp = Num[inner];
      Num[inner] = Num[inner - 1];
      Num[inner - 1] = tmp;
    }
We are going to place this info into a function called Insert().  Let's
do that now:
void Insert(int a[], int n)
{
  int outer, inner;  /* Insert Sort loop counters */
  int tmp;
  
  for (outer = 1; outer < n; outer++)     /* look for insertion point. */
    for (inner = outer; (inner > 0) && (a[inner - 1] > a[inner]); inner--) {
    /* Move the others down and insert it. */
      tmp = a[inner];
      a[inner] = a[inner - 1];
      a[inner - 1] = tmp;
    }
}
The variables are of no use inside the "main" function, because we use
them for the sort.  So we move them inside our "Insert" function.
Notice that we don't have to return any values back into "main".  All
the work is done inside the function.  We display the values in the
"main" function.  We only have to pass the "Num" array into "Insert".
We pass the array "Num[]" into "a[]".  You can use any name you want.
Even "Num[]", as long as it's the same type.
Our new program now looks like this:
/*------------------------------------------------------------------
 Program:       ISORT2.C
 Author:        CORVUS
 Date:          Sept. 17, 2001
 Description:   This is a program that sorts an array of 10 integers
                using the insert sort algorithm.
 -----------------------------------------------------------------*/
#include <stdio.h>
void Insert(int a[], int n);  /* Prototype */
int main(void)
{
  /* Initialize our array */
  int Num[10] = {234, 212, 0, 21, 14, 175, 998, 401, 1232, 110};
  int i;       /* Loop counter */
  for (i = 0; i < 10; i++)   /* Display the unsorted array */
    printf("%d\n",Num[i]);   /* on the screen. */
  Insert(Num, sizeof(Num) / sizeof(Num[0]));  /* Do an Insert Sort */
  /* print sorted array */
  printf("\n");
  printf("=====\n\n");
  for (i=0; i < 10; i++)
    printf("%d\n",Num[i]);
  return 0;
}
void Insert(int a[], int n)
{
  int outer, inner;  /* Insert Sort loop counters */
  int tmp;
  
  for (outer = 1; outer < n; outer++)     /* look for insertion point. */
    for (inner = outer; (inner > 0) && (a[inner - 1] > a[inner]); inner--) {
    /* Move the others down and insert it. */
      tmp = a[inner];
      a[inner] = a[inner - 1];
      a[inner - 1] = tmp;
    }
}
Some of this might need a bit of explaining.  We'll begin at the top, in
our "main" function.
Notice that when we call our "Insert" function, we pass TWO items of
data to it.  First we pass the array that needs to be sorted, and then
we pass the size of the array.  This is the interesting part and will
require a short talk on the sizes of different types.  Let's begin:
First of all, we'll begin with the "char" data type.  This type is
guaranteed to be 1 byte in size.  So, if you had: "char a[100]".  The
size of a is 100.  The other data types differ in size from system to
system.  There are some guarantees, but if you want to be sure, run the
following program to find out how many bytes each type is.
/*----------------------------------------------------------
 Program:       SZOF.C
 Author:        CORVUS
 Date:          Sept. 17, 2001
 Description:   This program uses sizeof() to get the size
                of various data types.
 ---------------------------------------------------------*/
#include<stdio.h>
int main(void)
{
  /* Show the sizes of data types. */
  printf("The size of short is: %d bytes\n", sizeof(short));
  printf("The size of int is: %d bytes\n", sizeof(int));
  printf("The size of long is: %d bytes\n", sizeof(long));
  printf("The size of float is: %d bytes\n", sizeof(float));
  printf("The size of double is: %d bytes\n", sizeof(double));
  return 0;
}
The output on my DOS system was:
The size of short is: 2 bytes
The size of int is: 2 bytes
The size of long is: 4 bytes
The size of float is: 4 bytes
The size of double is: 8 bytes
Whereas the output on my Win98 system was:
The size of short is: 2 bytes
The size of int is: 4 bytes
The size of long is: 4 bytes
The size of float is: 4 bytes
The size of double is: 8 bytes
The sizes on different OS's can range greatly.  For portability, never
assume that a data type is a certain size (except for char).
In our sorting program above, I used the size of "Num[]" which was 20
and divided it by the size of "Num[0]" which was 2 in my case.  That
resulted in the correct answer of 10 bytes.  (Assuming an int is 2
bytes on your system.)
So there you have it.  That was my little blurb on functions.  I highly
recommend you get a good book on C and review the part on functions.  It
will probably be written better than mine.
Let us continue onto Part 8.