Passing Arrays as Function Arguments


Previously in this chapter, we used the following program to demonstrate how loops are effective in assigning and displaying array values:

 #include <iostream> using namespace std; const int MAX = 3; int main () {  int testScore[MAX];  for (int i = 0; i < MAX; i++)  {  cout << "Enter test score #" << i + 1 << ": ";  cin >> testScore[i];  }  for (i = 0; i < MAX; i++)  {  cout << "Test score #" << i + 1 << ": "  << testScore[i] << endl;  }  return 0; } 

Now we are going to make this program more modular by writing one function to assign values to the array, and another function to display values from the array, rather than doing all that work in the main function.

 #include <iostream> using namespace std; void assignValues(int[], int); void displayValues(int[], int); const int MAX = 3; int main () {  int testScore[MAX];  assignValues(testScore, MAX);  displayValues(testScore, MAX);   return 0; } void assignValues(int tests[], int num) {  for (int i = 0; i < num; i++)  {  cout << "Enter test score #" << i + 1 << ": ";  cin >> tests[i];  } } void displayValues(int scores[], int elems) {  for (int i = 0; i < elems; i++)  {  cout << "Test score #" << i + 1 << ": "  << scores[i] << endl;  } } 

The assignValues function is used to assign values to the array. The displayValues function is used to display values from the array.

Each function has two arguments. The first argument is the array. The second argument is the number of elements in the array. Each function loops through the array, its first argument, using as an index limit the number of elements in the array, the second argument.

Since the first argument is not just an integer, but an array of integers, the argument is specified with brackets, [], signifying that what is being passed is an array.

Note  

You do not, and should not, put a number in the square brackets in the argument list of either the prototype or the function header.

There is one remaining question. The assignValues function changes the values in the array in main that was passed as its argument. As discussed in Chapter 9, for that to happen, the argument should be passed by reference rather than value. However, the array is not passed by reference.

Actually, Chapter 9 mentioned a third way of passing arguments: by address. Passing by address works the same way as passing by reference in that the called function can change in the calling function the value of a variable passed to it. As discussed previously in this chapter, the value of the name of an array is the base address of the array. Thus, in a function call such as assignValues(testScore, MAX), in which the first argument is the array name, the first argument is being passed by address. There will be much more on passing by address in Chapter 11.




C++ Demystified(c) A Self-Teaching Guide
C++ Demystified(c) A Self-Teaching Guide
ISBN: 72253703
EAN: N/A
Year: 2006
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net