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 bubble sort
algorithm into it's own function.  Here's the original program:
/*------------------------------------------------------------------
 Program:       BSORT.C
 Author:        CORVUS
 Date:          Aug. 21, 2001
 Description:   This is a program that sorts an array of 10 integers
                using the bubble 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, outer, inner;       /* Loop counters */
  int temp;                  /* temporary swap variable. */
  
  for (i = 0; i < 10; i++)   /* Display the unsorted array */
    printf("%d\n",Num[i]);   /* on the screen. */
  
  /* Bubble Sort our array
   from Biggest to Smallest */
    
  for (outer=0; outer < 9; outer++)        /* Loop while outer is */
    for (inner=9; inner > outer; inner--)  /* less than 9, (10 times) */
      if (Num[inner-1] < Num[inner]) {     /* and while inner is */
        temp=Num[inner-1];                 /* bigger than outer, */
        Num[inner-1]=Num[inner];           /* execute "if" */
        Num[inner]=temp;        
      } 
  /* 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 bubble sort area
itself:
  for (outer=0; outer < 9; outer++)        /* Loop while outer is */
    for (inner=9; inner > outer; inner--)  /* less than 9, (10 times) */
      if (Num[inner-1] < Num[inner]) {     /* and while inner is */
        temp=Num[inner-1];                 /* bigger than outer, */
        Num[inner-1]=Num[inner];           /* execute "if" */
        Num[inner]=temp;        
      } 
We are going to place this info into a function called Bubble().  Let's
do that now:
void Bubble(int Num[])
{
  int outer, inner;
  int temp;
  for (outer=0; outer < 9; outer++)        /* Loop while outer is */
    for (inner=9; inner > outer; inner--)  /* less than 9, (10 times) */
      if (Num[inner-1] < Num[inner]) {     /* and while inner is */
        temp=Num[inner-1];                 /* bigger than outer, */
        Num[inner-1]=Num[inner];           /* execute "if" */
        Num[inner]=temp;
      }
}
The variables are of no use inside the "main" function, because we use
them for the sort.  So we move them inside our "Bubble" 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.
Our new program now looks like this:
/*------------------------------------------------------------------
 Program:       BSORT2.C
 Author:        CORVUS
 Date:          Aug. 28, 2001
 Description:   This is a program that sorts an array of 10 integers
                using the bubble sort algorithm.
------------------------------------------------------------------*/
#include <stdio.h>
void Bubble(int Num[]);  /* 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. */
  Bubble(Num);    /* Do a Bubble Sort */
  /* print sorted array */
  printf("\n");
  printf("=====\n\n");
  for (i=0; i < 10; i++)
    printf("%d\n",Num[i]);
  return 0;
}
void Bubble(int Num[])
{
  int outer, inner;
  int temp;
  for (outer=0; outer < 9; outer++)        /* Loop while outer is */
    for (inner=9; inner > outer; inner--)  /* less than 9, (10 times) */
      if (Num[inner-1] < Num[inner]) {     /* and while inner is */
        temp=Num[inner-1];                 /* bigger than outer, */
        Num[inner-1]=Num[inner];           /* execute "if" */
        Num[inner]=temp;
      }
}
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.