/* demoArrayFunctions.c   P. Conrad   11/3/2004 *


#include <stdio.h>

/* 
Here's an example of passing an array into a function.

This is also an example of how to find the sum of all the
values in an array.

*/

int sumOfIntArray(int myArray[], int n)
{
  
  int i;
  int sum = 0;
  
  for (i=0; i<n; i++)
    sum += myArray[i];
  
  return sum;
  
}


/* 

In your main, if you declare the function AFTER the main,
you'll need a function prototype as follows.  Here, the
function prototype is not strictly needed, but it doesn't 
hurt to include it.

*/

int sumOfIntArray(int myArray[], int n);


/* 

So the names of the formal parameters of this function prototype are:

    myArray
    n

When you call the function, the first ACTUAL parameter needs to be
the name of an array.

The second actual parameter needs to be an integer expression which
tells the function how many values are in the array.

Now, in your main, you'll call it this way:

*/

int main(void)
{
  
  /* The following declares an array.  I've initialized
     values in the declaration just to keep the example short.
     However, this code would still work, even if the array were
     given values some other way, e.g. assigned from scanf calls,
     or read from a file, or whatever. */
  
  int exampleArray[5] = {23, 45, 62, 16, 78};
  
  int total;
  
  total = sumOfIntArray(exampleArray ,  5   );
  
  /* The actual parameters of this function call to the
     function named "sumOfIntArray" are "exampleArray" and "5".  */


  printf("The sum of the array is %d\n", total);

}


/* One more thing... this program would work equally well if
we used the following syntax in the function header and/or
function prototype: 

  int sumOfIntArray(int *myArray, int n);

That's because the name of an array is a POINTER to the
first element (i.e. myArray[0]), and we can therefore declare
it as a variable of type int *.

We'll talk more about that in lecture.

*/

