In some applications we may want to pass complete arrays to functions. An array name can be used as an argument to a function, thus permitting the entire array to be passed. To pass a complete array to a function, the array name must appear by itself within the brackets. The size of the array is not specified within the formal argument declaration. In the function header the array name must be specified with a pair of empty brackets. It is important to realize that when a complete array is passed to a function, what is actually passed is not a copy of the array but the address of the first element of the array (i.e., the array elements are passed by reference, which means that the original array elements can be modified inside the function).
Some examples follow that illustrate the passing of a complete array to a function.
Write a program to store the numbers 1 to 10 in an array called Numbers. Then call a function named Average to calculate the average of these numbers.
The required program listing is shown in Figure 4.8. Function Average receives the elements of array Numbers and calculates the average of the array elements.
/********************************************************************
PASSING AN ARRAY TO A FUNCTION
==============================
This program stores numbers 1 to 10 in an array called Numbers. Function
Average is then called to calculate the average of these numbers.
Programmer: Dogan Ibrahim
File: AVERAGE.C
Date: May, 2007
*********************************************************************/
/* Function to calculate the average */
float Average(int A[]) {
float Sum = 0.0, k;
unsigned char j;
for (j=0; j<10; j++) {
Sum = Sum + A[j];
}
k = Sum / 10.0;
return k;
}
/* Start of the main program */
void main() {
unsigned char j;
float Avrg;
int Numbers[10];
for (j=0; j<10; j++) Numbers[j] = j+1;
Avrg = Average(Numbers);
}
Figure 4.8: Program passing an array to a function
Repeat Example 4.6, but this time define the array size at the beginning of the program and then pass the array size to the function.
The required program listing is shown in Figure 4.9.
/*********************************************************************
PASSING AN ARRAY TO A FUNCTION
==============================
This program stores numbers 1 to N in an array called Numbers where N is
defined at the beginning of the program. Function Average is then called to
calculate the average of these numbers.
Programmer: Dogan Ibrahim
File: AVERAGE2.C
Date: May, 2007
***********************************************************************/
#define Array_Size 20
/* Function to calculate the average */
float Average(int A[], int N) {
float Sum = 0.0, k;
unsigned char j;
for (j=0; j<N; j++) {
Sum = Sum + A[j];
}
k = Sum / N;
return k;
}
/* Start of the main program */
void main() {
unsigned char j;
float Avrg;
int Numbers[Array_Size];
for (j=0; j<Array_Size; j++) Numbers[j] = j+1;
Avrg = Average(Numbers, Array_Size);
}
Figure 4.9: Another program passing an array to a function
It is also possible to pass a complete array to a function using pointers. The address of the first element of the array is passed to the function, and the function can then manipulate the array as required using pointer operations. An example follows.
Repeat Example 4.6, but this time use a pointer to pass the array elements to the function.
The required program listing is given in Figure 4.10. An integer pointer is used to pass the array elements to the function, and the function elements are manipulated using pointer operations. Notice that the address of the first element of the array is passed as an integer with the statement: &Numbers[0].
/********************************************************************
PASSING AN ARRAY TO A FUNCTION
==============================
This program stores numbers 1 to 10 in an array called Numbers. Function
Average is then called to calculate the average of these numbers.
Programmer: Dogan Ibrahim
File: AVERAGE3.C
Date: May, 2007
*********************************************************************/
/* Function to calculate the average */
float Average(int *A) {
float Sum = 0.0, k;
unsigned char j;
for (j=0; j<10; j++) {
Sum = Sum + *(A + j);
}
k = Sum / 10.0;
return k;
}
/* Start of the main program */
void main() {
unsigned char j;
float Avrg;
int Numbers[10];
for(j=0; j<10; j++) Numbers[j] = j+1;
Avrg = Average(&Numbers[0]);
}
Figure 4.10: Program passing an array using pointers
4.1.3 Passing Variables by Reference to Functions
By default, arguments to functions are passed by value. Although this method has many distinct advantages, there are occasions when it is more appropriate and also more efficient to pass the address of the arguments instead, that is, to pass the argument by reference. When the address of an argument is passed, the original value of that argument can be modified by the function; thus the function does not have to return any variables. An example follows which illustrates how the address of arguments can be passed to a function and how the values of these arguments can be modified inside the function.