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:
#include <iostream>
: This includes the standard C++ library for input/output operations.using namespace std;
: This allows the use of standard identifiers without requiring thestd::
prefix.Two function prototypes are declared:
DisplayArray(string[], int)
andCopyArray(string[], string[], int)
. These are the functions that will be defined later in the code.DisplayArray
will print the contents of a given array, andCopyArray
will copy the contents of one array to another.const int ARRAY_SIZE = 5;
: This declares a constant integerARRAY_SIZE
and assigns it the value of5
.Two string arrays,
studentName
andstudentNameTwo
, are declared, andstudentName
is initialized with five names.DisplayArray(studentName, ARRAY_SIZE);
: TheDisplayArray
function is called to display the contents of thestudentName
array.CopyArray(studentName, studentNameTwo, ARRAY_SIZE);
: TheCopyArray
function is called to copy the contents of thestudentName
array to thestudentNameTwo
array.DisplayArray(studentNameTwo, ARRAY_SIZE);
: TheDisplayArray
function is again called to display the contents of thestudentNameTwo
array, which should now be identical to thestudentName
array.The function
DisplayArray
loops over the array passed to it, printing each index and its corresponding value.The function
CopyArray
loops over thepArray
(source array), copying each value into the corresponding index inpCopyArray
(destination array).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