## Functions and Arrays

There are many situations in which it's natural to ask for functions that have arrays as arguments or that return arrays ... or both! This class is just going to look at functions that operate on arrays, and the few new concepts that arise with them. In reality, there's nothing new at all ... it's just that the consequences of the fact that it is the pointers to the arrays that get manipulated in our programs, not the arrays themselves may not be so obvious.

We'll look at this stuff first from the prespective of our simple program from last class for computing averages. Here's that program. We're going to try and break it up into separate componants using functions. Essentially we'd like the bulk of the program to consist of these two function calls:

// Create array A and read N int's from input

// Compute average
double average = sum(A,N) / double(N);

In defining these two functions, we'll see how arrays get passed in and out of functions.

## Returning Arrays from Functions

The first of our two functions is int* readints(int N, istream &IN);. This function should return an array of N ints, populated with values read in from IN. The basics of the function should be fairly straightforward. The important thing is to understand what happens! So, here's the function definition:

int* readints(int N, istream &IN) {
// Create the array
int* B = new int[N];

// Read values into the array
for(int i = 0; i < N; i++)
IN >> B[i];

// Return pointer to array
return B;
}

And here's a pictorial depiction of what goes on. Notice that what gets returned from the function is a point, not an actual array. The array of ints does not disappear when the function call returns, because memory allocated with new has no scope or lifetime ... it just sticks around until the end of the program, or until it's explicitly destroyed with a call to delete, which we'll discuss later.

So, to sum up, functions can create arrays with new and return them by returning a pointer to the block of memory allocated by the call to new.

## Passing Arrays to Functions

Arrays can be passed into functions by passing a pointer to the block of memory that comprises the array. Notice what this means as far as pass-by-value is concerned: the pointer is what gets passed, so your function gets a copy of the pointer, but the copy points to the same array in memory, so when you modify the array in the function you modify the same array you have back in the calling function. Thus, some people will say that arrays are always passed by reference in C++. Well ... really what happens is that arrays themselves are not passed, rather pointers to the arrays are what we pass, and receiving a copy of the pointer still allows us to modify the actual array.

So, getting back to our example, we need to implement the function int sum(int* A, int N);. Notice that when we pass an array to a function we always have to pass the length of the array along with it, otherwise we have no idea how many elements are in the array pointed to by A. Hopefully the definition of this function looks straightforward.

int sum(int* A, int N) {
int total = 0;
for(int i = 0; i < N; i++)
total = total + A[i];
}

The question is this: What really goes on when we run it? Hopefully the pictures below, which illustrate what goes on in a sample run of the above function, will make clear to you what really goes on when arrays are passed to functions.

## Modifying Arrays Passed to Functions

Hopefully the above functions make it pretty clear that if you modify any element within an array passed to a function, you're actually modifying the same array that the caller of the function is looking at.

## Problems

1. A contains predicate. Write function bool contains(string *A, int N, string s); that tells you whether or not the string s is contained in the array A.
2. A getarray function. Write a function int* getarray(int N, int x); that allocates and returns a pointer to an array of N ints, each initialized to the value x.
3. A shiftleft function. Write a function double shiftleft(double *A, int N, double x); that shifts all the elements in array A to the left by 1 position, puts the value x in the rightmost position in the array, and returns the value that had previously been in the leftmost position.
4. An average function for arrays of doubles.
5. Use the average function and the shiftleft function to do the smoothing problem from the last lab. Here's my solution.