|Lessons||Lesson 17: Functions and Arrays|
Now let’s see what happens when we pass an array to a function. To pass an array to a function, we will tell the function two things about the array i.e. the name of the array and the size. The size of the array is necessary to pass to the function. As the array is declared in the calling function, it is visible there. The calling function knows its size but the function being called does not know the size of the array. So it is necessary to pass the size of the array along with its name. Suppose we have declared a character array in the program by the following statement:
char name ;
We have a function, say reverse that reverses the array elements and displays them. Firstly, we need to write the prototype of the function reverse. We say that this function returns nothing so we use the keyword void in its return type. Secondly, we have to write the parameters this function will get. We write these parameters with their type. Now the prototype of this function will be written as
void reverse ( char , int ) ;
In the above statement, the brackets  are necessary. These brackets
indicate that an array of type char will be passed to the function. If
we skip these brackets and simply write char, it will mean that a single
character will be passed to the function. In addition, the second parameter
i.e. of type int, is of array's size. Note that in the prototype of the
function we have not written the names of the parameters. It is not necessary
to write the names of the parameters in function prototype. However, if
we write the names, it is not an error. The compiler will simply ignore
void reverse ( char characters
, int arraySize )
You are encouraged to try to write the function body for yourself.
Let’s say we have a character array name and a name ‘adnan’ is stored in it. We call the reverse function by passing the array name to it. For this we write
reverse ( name, 100 );
In this function call, we are sending the name of the array to the function i.e. name and the size of the array that is 100. When this call of the function is executed the control goes to the function reverse. The statements in this function are executed which reverses the array and displays it. After this, the control comes back to the main function to the statement next to the function call statement. The return type of the function is void so it does not return any thing. Now in the main, we write the statement:
cout << name;
What will be displayed by this statement? Whether it will be the original name ‘adnan’ or something else. It will display the reversed array. In this instance, we see that whatever the function reverse did to the array ( that was passed to it) is appearing in the calling function. It means that the original array in the calling program has been changed. Here we change (reverse) the order of the characters of array in the function and find that the characters of the array in the calling function are reversed. This means that the called function has not a copy of the array but has the original array itself. Whereas in case of simple variables, a called function uses a copy of variables passed to it in a 'call by value' mechanism, which is by default in case of simple variables. In arrays, the by default mechanism is ‘call by reference’. While passing arrays to a function, we don’t need to use & and * operators, as we use for variables in call by reference mechanism. Thus if we pass an array to a function, the array itself is passed to the function. This is due to the fact that when we declare an array, the name of the array has the address of the memory location from where the array starts. In other words, it is the address of the first element of the array. Thus the name of the array actually represents the address of the first location of the array. Passing the name of array to a function means the passing of the address of the array which is exactly the same as we do in call by reference. So whatever the function does to the array, it is happening in the same memory locations where the array originally resides. In this way, any modifications that the function does to the contents of the array are taking place in the contents of the original array too. This means that any change to the array made by the function will be reflected in the calling program. Thus an important point to remember is that whenever we pass simple variables to a function, the default mechanism is call by value and whenever we pass an array to a function, the default mechanism is call by reference. We know that when we talk about a single element of an array like x  (which means the fourth element of the array x), it is treated as simple variable. So if we pass a single element of an array to a function (let’s say like fn ( x  ); ), it is just like a simple variable whose copy is passed to the function (as it is a call by value). The original value of the element in the array remains the same. So be careful while passing arrays and a single element of array to functions. This can be well understood from the following example.
Suppose we declare an array in the main program and pass this array to a function, which populates it with values. After the function call, we display the elements of the array and see that it contains the values that were given in the function call. This demonstrates that the called function changes the original array passed to it. Following is the code of the program.
/*This program demonstrates
that when an array is passed to a function then it is a call by reference
and the changes made by the function effects the original array*/
Here in the function getvalues, we can get the values of the array from user by using the cin statement. Following is the output of the execution of the program.
The array is populated
NEXT>>>>>Lesson 18. Multidimensional Arrays