Back in chapter 5 when we studied functions, I hinted to you that there was a way to get data back from a function by using an array, and that is true. Load the program passback.c for an example of doing that.
main( )
{
int index;
int matrix[20];
for (index = 0;index < 20;index++) /* generate data */
matrix[index] = index + 1;
for (index = 0;index < 5;index++) /* print original data */
printf("Start matrix[%d] = %d\n",index,matrix[index]);
dosome(matrix); /* go to a function & modify matrix */
for (index = 0;index < 5;index++) /* print modified matrix */
printf("back matrix[%d] = %d\n",index,matrix[index]);
}
dosome(list) /* This will illustrate returning data */
int list[];
{
int i;
for (i = 0;i < 5;i++)
printf("Before marrix[%d] = %d\n",i,list[i]);
for (i = 0;i < 20;i++) /* add 10 to all values */
list[i] +=10;
for (i =0;i < 5;i++) /* print modified matrix */
printf("After matrix[%d] = %d\n",i,list[i]);
}
In this program, we define an array of 20 variables named "matrix", then assign some nonsense data to the variables, and print out the first five. Then we call the function "dosome" taking along the entire array by putting the name of the array in the parentheses. The function "dosome" has a name in its parentheses also but it prefers to call the array "list". The function needs to be told that it is really getting an array passed to it and that the array is of type "int". The following line, prior to the bracket which starts the program, does that by
defining "list" as an integer type variable and including the square brackets to indicate an array.
It is not necessary to tell the function how many elements are in the array, but you could if you so desired. Generally a function works with an array until some end-of-data marker is found, such as a NULL for a string, or some other previously defined data or pattern. Many times, another piece of data is passed to the function with a count of how many elements to work with. On our present illustration, we will use a fixed number of elements to keep it simple. So far nothing is different from the previous functions we have called except that we have passed more data points to the function this time than we ever have before, having passed 20 integer values. We print out the first 5 again to see if they did indeed get passed here. Then we add
ten to each of the elements and print out the new values. Finally we return to the main program and print out the same 5 data points. We find that we have indeed modified the data in the function, and when we returned to the main program, we brought the changes back.
Compile and run this program to verify this conclusion.
main( )
{
int index;
int matrix[20];
for (index = 0;index < 20;index++) /* generate data */
matrix[index] = index + 1;
for (index = 0;index < 5;index++) /* print original data */
printf("Start matrix[%d] = %d\n",index,matrix[index]);
dosome(matrix); /* go to a function & modify matrix */
for (index = 0;index < 5;index++) /* print modified matrix */
printf("back matrix[%d] = %d\n",index,matrix[index]);
}
dosome(list) /* This will illustrate returning data */
int list[];
{
int i;
for (i = 0;i < 5;i++)
printf("Before marrix[%d] = %d\n",i,list[i]);
for (i = 0;i < 20;i++) /* add 10 to all values */
list[i] +=10;
for (i =0;i < 5;i++) /* print modified matrix */
printf("After matrix[%d] = %d\n",i,list[i]);
}
In this program, we define an array of 20 variables named "matrix", then assign some nonsense data to the variables, and print out the first five. Then we call the function "dosome" taking along the entire array by putting the name of the array in the parentheses. The function "dosome" has a name in its parentheses also but it prefers to call the array "list". The function needs to be told that it is really getting an array passed to it and that the array is of type "int". The following line, prior to the bracket which starts the program, does that by
defining "list" as an integer type variable and including the square brackets to indicate an array.
It is not necessary to tell the function how many elements are in the array, but you could if you so desired. Generally a function works with an array until some end-of-data marker is found, such as a NULL for a string, or some other previously defined data or pattern. Many times, another piece of data is passed to the function with a count of how many elements to work with. On our present illustration, we will use a fixed number of elements to keep it simple. So far nothing is different from the previous functions we have called except that we have passed more data points to the function this time than we ever have before, having passed 20 integer values. We print out the first 5 again to see if they did indeed get passed here. Then we add
ten to each of the elements and print out the new values. Finally we return to the main program and print out the same 5 data points. We find that we have indeed modified the data in the function, and when we returned to the main program, we brought the changes back.
Compile and run this program to verify this conclusion.

