Using arrays as function arguments:

Arrays can be passed down to functions like other single variables. We use them by their name as arguments when we call the function. The parameter list of the function must indicate the fact that a parameter is an array by augmenting the [ ] at the end of the one dimensional arrays. In one-dimensional arrays the actual size does not have to be given. In case of two-dimensional arrays we recommend that you give the size of the array in both dimensions.

NOTE: Arrays are always passed by name. In other words if we change the array in the function, then this change reflects back to the calling function.

Read the explanations provided for the example .

 
 /*
      J.Y.Kazakia October 14,1998
 
This example illustrates the following:
 
1) Arrays in C++.
 
2) Arrays passed to a function return their updated values to the main.
 
Here a set of four numbers is passed to a function called square_them first individually and then as an array. The results in both cases are then compared. The function forms the squares of the numbers.
 
                                */
#include <iostream.h>
#include <fstream.h>
The function has five parameters.  The first four are single variables passed by value. The fifth parameter is an array. As we mentioned earlier, arrays are passed by name.
  /* function prototypes */
 
void square_them ( float a, float b, float c, float d, float array[]);
 
ofstream out( "6ex3rep.txt" , ios:: out);
 
main ( )
 
{
The variables are declared and the regular variables a, b, c, d are initialized one by one.
float a, b, c, d;
float array[4];
int i;
 
a = 1.1;
b = 2.2;
c = 3.3;
d = 4.4;
The array can be initialized using a loop
for ( i = 0; i <= 3; i++)
array [ i ] =(1+ i) +( i +1 ) / 10.;
 
/* this is equivalent to writing: array[0] = 1.1;
                                  array[1] = 2.2;
                                  array[2] = 3.3;
                                  array[3] = 4.4;
Values of the variables are printed out to the file called 6ex3rep.txt via the stream out, before the call to the function. We do this so that we can compare these values to the ones after the call.
   Print before you call the function    */
out << " \n\n\n  Printing before  calling the function";
out << endl << endl;
out << " a =" << a << " b =" << b << " c =" << c ;
out <<" d =" << d ;
 
for ( i = 0; i <= 3; i++)
out <<"\n\n   array["<<i<<"] = "<<array[i];
The function is called.  Notice that the array goes in the argument list with only its name ( no brackets).
/*****************************************
 
   Call the function
 
*****************************************/
 
square_them( a, b, c, d, array );
Values of the variables are printed out to the file called 6ex3rep.txt via the stream out, after the call to the function
/*   Print after  you call the function    */
 
out << " \n\n\n  Printing after  calling the function";
out << endl << endl;
out << " a =" << a << " b =" << b << " c =" << c ;
out <<" d =" << d ;
 
for ( i = 0; i <= 3; i++)
out <<"\n\n   array["<<i<<"] = "<<array[i];
 
 
}
This is the definition of the function. The task of the function is to calculate the squares of all variables
 void square_them( float a, float b, float c, float d, float u[])
 
{
int i;
 
a = a * a;
b = b * b;
c = c * c;
d = d * d;
 
i=0;
while (i <= 3)
  {
     u[i] = u[i] * u[i];
     i++;
We print the values from within the function. Thus we are going to have three printings:
a)      before the call
b)      from the function
c)      after the call
/*   Print from within the function    */
 
out << " \n\n\n  Printing from within the function";
out << endl << endl;
out << " a =" << a << " b =" << b << " c =" << c ;
out <<" d =" << d ;
 
for ( i = 0; i <= 3; i++)
out <<"\n\n   array["<<i<<"] = "<< u[i];
 
}
Note the following:
 
Before the call to the function we have the values which we assigned.
 
From within the function square_them, we get the squares of all variables
 
 
From the function main when we print the variables after the call to the function square_them we get the "unsquared " values for a, b, c, d but the squared values for the array elements.
 
This is because the single variables a, b, c, d are passed by valu( no & is placed before them) but the array always passes by name.
THE OUTPUT IS:
 
  Printing before  calling the function
 
 a =1.1 b =2.2 c =3.3 d =4.4
 
   array[0] = 1.1
 
   array[1] = 2.2
 
   array[2] = 3.3
 
   array[3] = 4.4
 
 
  Printing from within the function
 
 a =1.21 b =4.84 c =10.89 d =19.36
 
   array[0] = 1.21
 
   array[1] = 4.84
 
   array[2] = 10.89
 
   array[3] = 19.36
 
 
  Printing after  calling the function
 
 a =1.1 b =2.2 c =3.3 d =4.4
 
   array[0] = 1.21
 
   array[1] = 4.84
 
   array[2] = 10.89
 
   array[3] = 19.36
 
 
   */

(text file of the above code - 6ex3)

(printable file)

© 2001 J.Y. Kazakia. All rights reserved