#include using namespace std; const int MAX_SIZE{10}; void PassByValue(int x); void PassByReference(int & x); void PassByConstantReference (const int & x); void PassByPointer(int data[], int size); void PassByConstPointer(const int data[], int size); // utility function, ignore void PrintArray(const int data[], int size); int main() { int a{100}; int myArray[MAX_SIZE]{1,2,3,4,5,6,7,8,9,10}; const int constArray[MAX_SIZE]{1,2,3,4,5,6,7,8,9,10}; cout << "In main" << endl; cout << "\tMain: a = " << a << endl; cout << "\tCalling PassByValue(a)" << endl; cout << endl; PassByValue(a); cout << endl; cout << "Back in Main: " << endl; cout << "\tMain: a = " << a << endl; cout << "\tCalling PassByReference(a)" << endl; cout << endl; PassByReference(a); cout << endl; cout << "Back in Main: " << endl; cout << "\tMain: a = " << a << endl; cout << "\tCalling PassByConstReference(a)" << endl; cout << endl; PassByConstantReference(a); cout << endl; cout << "Back in Main: " << endl; cout << "\tMain: a = " << a << endl; cout << "\tThe array is " ; PrintArray(myArray, 10); cout << "Calling PassByPointer(myArray, 10)" << endl; cout << endl; PassByPointer(myArray, 10); cout << endl; cout << "Back in Main: " << endl; cout << "\tThe array is " ; PrintArray(myArray, 10); cout << "Calling PassByConstPointer(myArray, 10)" << endl; cout << endl; PassByConstPointer(myArray, 10); cout << endl; cout << "Back in Main: " << endl; cout << "\tThe array is " ; PrintArray(myArray, 10); cout << endl; // some experiments with constants PassByValue(100); //PassByReference(100); PassByConstantReference(100); //PassByPointer(constArray,10); //PassByConstPointer(constArray,10); PrintArray(constArray,10); return 0; } void PassByValue(int x){ cout << "In PassByValue" << endl; cout <<"\tx = " << x << endl; x = -999; cout << "\tSetting x to be " << x << endl; return; } void PassByReference(int & x) { cout << "In PassByReference" << endl; cout <<"\tx = " << x << endl; x = -999; cout << "\tSetting x to be " << x << endl; return; } void PassByConstantReference (const int & x){ cout << "In PassByConstantReference" << endl; cout <<"\tx = " << x << endl; // The next line may be commented out so the program will compile. //x = 10; cout << "\tSetting x to be " << x << endl; return; } void PrintArray(const int data[], int size){ int i; if (size > 0) { cout << data[0]; for(i = 1; i < size; ++i) { cout << ", " << data[i]; } cout << endl; } return; } void PassByPointer(int data[], int size) { int i; cout << "In PassByPointer" << endl; cout << "\tthe array is "; PrintArray(data,size); for(i = 0; i < size; ++i) { data[i] = data[i] + 10; } cout << "\tAfter adding 10 to each element " << endl; cout << "\tthe array is "; PrintArray(data,size); return; } void PassByConstPointer(const int data[], int size) { int i; cout << "In PassByPointer" << endl; cout << "\tthe array is "; PrintArray(data,size); for(i = 0; i < size; ++i) { // The next line might be commented out so the code will compile //data[i] = data[i] + 10; } cout << "\tAfter adding 10 to each element " << endl; cout << "\tthe array is "; PrintArray(data,size); return; }