Demo - Arrays as Parameters to Functions

// Use a function to Copy an Array - note - Arrays are "pass by reference" #include <iostream> using namespace std; //prototype our functions void DisplayArray(string[], int); void CopyArray(string[], string[], int); int main() { // Declare variables const int ARRAY_SIZE = 5; //global variable for Array Size //initialize and Array for students string studentName[ARRAY_SIZE] = { "Kevin", "Fred", "Sally", "Cecilia", "Sam" }; string studentNameTwo[ARRAY_SIZE]; cout << "Lets look at the Original Array" << endl; DisplayArray(studentName, ARRAY_SIZE); cout << "Now lets copy the Array using the function we created" << endl; CopyArray(studentName, studentNameTwo, ARRAY_SIZE); cout << "Lets look output our copied Array" << endl; DisplayArray(studentNameTwo, ARRAY_SIZE); return 0; } //end of main void DisplayArray(string pArray[], int pSize) { cout << "Displaying the contents of my Array) " << endl; for(int i = 0; i < pSize; i++) { cout << "Index "<< i << " is " << pArray[i] << endl; } } // end of Function DisplayArray void CopyArray(string pArray[], string pCopyArray[], int pSize) { //now lets copy element by element for(int i = 0; i < pSize; i++) { pCopyArray[i] = pArray[i]; } } // end of Function CopyArray

This C++ code performs a task similar to the previous example - copying data from one array to another - but this time, the actions are encapsulated into functions. This makes the main() function cleaner and easier to read.

Here's a step-by-step explanation:

  1. #include <iostream>: This includes the standard C++ library for input/output operations.

  2. using namespace std;: This allows the use of standard identifiers without requiring the std:: prefix.

  3. Two function prototypes are declared: DisplayArray(string[], int) and CopyArray(string[], string[], int). These are the functions that will be defined later in the code. DisplayArray will print the contents of a given array, and CopyArray will copy the contents of one array to another.

  4. const int ARRAY_SIZE = 5;: This declares a constant integer ARRAY_SIZE and assigns it the value of 5.

  5. Two string arrays, studentName and studentNameTwo, are declared, and studentName is initialized with five names.

  6. DisplayArray(studentName, ARRAY_SIZE);: The DisplayArray function is called to display the contents of the studentName array.

  7. CopyArray(studentName, studentNameTwo, ARRAY_SIZE);: The CopyArray function is called to copy the contents of the studentName array to the studentNameTwo array.

  8. DisplayArray(studentNameTwo, ARRAY_SIZE);: The DisplayArray function is again called to display the contents of the studentNameTwo array, which should now be identical to the studentName array.

  9. The function DisplayArray loops over the array passed to it, printing each index and its corresponding value.

  10. The function CopyArray loops over the pArray (source array), copying each value into the corresponding index in pCopyArray (destination array).

  11. The program ends by returning 0, indicating a successful execution.

The key takeaway from this code is that arrays in C++ are passed by reference, not by value, which means any changes made to an array within a function will persist after the function call ends. This behavior allows the CopyArray function to modify the studentNameTwo array directly.

COSC-1336 / ITSE-1302 Computer Science - Author: Dr. Kevin Roark