Friday, September 25, 2009

array and functions

1.1 Arrays
If you want a number of variables of the same type in a list, the simplest way to do this is with an array. To create an array, you must declare the variable as normal, but put the number of items in the array in square brackets after the variable name, for example:
int Month[12];
This declares an array called ‘Month’, which consists of 12 integers. You can then read and write each one separately, again by using the index in square brackets. Note that arrays start at zero, so Month actually goes from 0 to 11, not 1 to 12. To assign to one item in the array:
Month[0] = 31;
Month[1] = 28;
And so on…
1 Functions
Functions allow you to group your code into smaller subroutines, that you can then call to execute them. Functions are declared as:
FunctionName( Parameter1, Parameter2)
If you don’t want to return a value, or you don’t want any parameters, use the type void.
Every C program must have at least one function, called main. This returns an int, and takes void. (It can take other things, but for programming on a PIC you need not worry about this.)Use of functions is probably easiest to see with an example