Erin's CS stuff

CS stuff!

Arrays in Functions

Because arrays are for groups of the same type of values and parameters can only store one thing, passing arrays into functions is a little different...

1D Array Parameters

Just a quick syntax reminder about arrays and variables: the name of an array is "bound" to or refers to the starting address of the collection of values. This means that arrays are always passed by address. This is just part of how C (and many other languages) work.

Prototypes

Because the array parameter just stores a memory address, to be able to access all elements in the array, we will also need a parameter for size or length.

void displayDoubleArray(int length, const double array[]);

IMPORTANT! There should never be anything in the square brackets. Even if you include a number or variable there, it will not have any effect at all on the behavior of the program. The length value should always be passed in as an additional parameter. [1]

Definitions

When the length value is passed in properly, it can be used as the upper bound (or limit) in the for loop:

void displayDoubleArray(int length, const double array[]){
  for(int index = 0; index < length; index++){
    printf(".2lf ", array[index]);
  }
}

NOTE! While you should also include the length value when working with C-style Strings, it's not strictly necessary because the null character \0 controls the for loop.

Calls

Let's assume we have an array for hourly temperature measurements for one day, and a macro was used during that array declaration:

double temperatures[MEASUREMENTS];

Then our function call would include the macro and just the array name as arguments.

displayDoubleArray(MEASUREMENTS, temperatures);

2D Array Parameters

Before we dive in, you may want to review some of their technical details of 2D Array Behavior.

Prototypes

For 2D array parameters, we will also need parameters for both the row size and the column size!

void clearBoard(int numRows, int numCols, char board[][numCols]);
IMPORTANT! There must always be a value in the second set of square brackets (but never the first). Generally, this is a reference to the column size parameter so it must come before the array in the function's list of parameters. The description of 2D Array Behavior explains why the value in the column size is necessary in the second set of square brackets.

Definitions

When the row and column sizes are passed in properly, they can be used as the upper bounds (or limit) in each of the for loops:

void clearBoard(int numRows, int numCols, char board[][numCols]){
  for(int rowIndex = 0; rowIndex < numRows; rowIndex++){
    for(int colIndex = 0; colIndex < numCols; colIndex++){
      board[rowIndex][colIndex] = ' ';
    }
  }
}
NOTE! While you should also include the column size as a parameter when working with C-style Strings, it's not strictly necessary because the null character \0 controls the inner for loop.
However, you still need a value in the second set of square brackets to compile! In that case, the string capacity macro can be used, since it is defined in the global space.

Calls

Let's assume we have a 2D array for a TicTacToe board, and a macro was used during that array declaration:

char ticTacToe[SIZE][SIZE];

Then our function call would include the macro and just the array name as arguments.

clearBoard(SIZE, SIZE, ticTacToe);

1. Since array parameters are inheritly passed by address, any modifications to the elements within the function persist (meaning changes to the element values don’t disappear after the function is done). The const keyword prevents the array from being modified within the function, providing a sort of “read-only” protection against writing to the array.