Programming in C

(Barry) #1
Functions and Arrays 143

When you were examining Program 8.11, your attention surely must have been drawn
to the following statement:


array[i] *= 2;


The effect of the “times equals” operator (*=) is to multiply the expression on the left
side of the operator by the expression on the right side of the operator and to store the
result back into the variable on the left side of the operator.So, the previous expression is
equivalent to the following statement:


array[i] = array[i] * 2;


Getting back to the main point to be made about the preceding program, you might
have realized by now that the function multiplyBy2actually changesvalues inside the
floatValsarray. Isn’t this a contradiction to what you learned before about a function
not being able to change the value of its arguments? Not really.
This program example points out one major distinction that must always be kept in
mind when dealing with array arguments: If a function changes the value of an array
element, that change is made to the original array that was passed to the function.This
change remains in effect even after the function has completed execution and has
returned to the calling routine.
The reason an array behaves differently from a simple variable or an array element—
whose value cannotbe changed by a function—is worthy of explanation. As mentioned
previously, when a function is called, the values that are passed as arguments to the func-
tion are copied into the corresponding formal parameters.This statement is still valid.
However, when dealing with arrays, the entire contents of the array are notcopied into
the formal parameter array. Instead, the function gets passed information describing where
in the computer’s memory the array is located. Any changes made to the formal parame-
ter array by the function are actually made to the original array passed to the function,
and not to a copy of the array.Therefore, when the function returns, these changes still
remain in effect.
Remember, the discussion about changing array values in a function applies only to
entire arrays that are passed as arguments, and not to individual elements, whose values
are copied into the corresponding formal parameters and, therefore, cannot be perma-
nently changed by the function. Chapter 11 discusses this concept in greater detail.


Sorting Arrays


To further illustrate the idea that a function can change values in an array passed as an
argument, you will develop a function to sort (rank) an array of integers.The process of
sorting has always received much attention by computer scientists, probably because sort-
ing is an operation that is so commonly performed. Many sophisticated algorithms have
been developed to sort a set of information in the least amount of time, using as little of
the computer’s memory as possible. Because the purpose of this book is not to teach
such sophisticated algorithms, you develop a sortfunction that uses a fairly straightfor-
ward algorithm to sort an array into ascending order. Sorting an array into ascending order

Free download pdf