20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Functions and Arrays<br />

143<br />

When you were exam<strong>in</strong><strong>in</strong>g Program 8.11, your attention surely must have been drawn<br />

to the follow<strong>in</strong>g statement:<br />

array[i] *= 2;<br />

The effect of the “times equals” operator (*=) is to multiply the expression on the left<br />

side of the operator by the expression on the right side of the operator and to store the<br />

result back <strong>in</strong>to the variable on the left side of the operator.So, the previous expression is<br />

equivalent to the follow<strong>in</strong>g statement:<br />

array[i] = array[i] * 2;<br />

Gett<strong>in</strong>g back to the ma<strong>in</strong> po<strong>in</strong>t to be made about the preced<strong>in</strong>g program, you might<br />

have realized by now that the function multiplyBy2 actually changes values <strong>in</strong>side the<br />

floatVals array. Isn’t this a contradiction to what you learned before about a function<br />

not be<strong>in</strong>g able to change the value of its arguments? Not really.<br />

This program example po<strong>in</strong>ts out one major dist<strong>in</strong>ction that must always be kept <strong>in</strong><br />

m<strong>in</strong>d when deal<strong>in</strong>g with array arguments: If a function changes the value of an array<br />

element, that change is made to the orig<strong>in</strong>al array that was passed to the function.This<br />

change rema<strong>in</strong>s <strong>in</strong> effect even after the function has completed execution and has<br />

returned to the call<strong>in</strong>g rout<strong>in</strong>e.<br />

The reason an array behaves differently from a simple variable or an array element—<br />

whose value cannot be changed by a function—is worthy of explanation. As mentioned<br />

previously, when a function is called, the values that are passed as arguments to the function<br />

are copied <strong>in</strong>to the correspond<strong>in</strong>g formal parameters.This statement is still valid.<br />

However, when deal<strong>in</strong>g with arrays, the entire contents of the array are not copied <strong>in</strong>to<br />

the formal parameter array. Instead, the function gets passed <strong>in</strong>formation describ<strong>in</strong>g where<br />

<strong>in</strong> the computer’s memory the array is located. Any changes made to the formal parameter<br />

array by the function are actually made to the orig<strong>in</strong>al array passed to the function,<br />

and not to a copy of the array.Therefore, when the function returns, these changes still<br />

rema<strong>in</strong> <strong>in</strong> effect.<br />

Remember, the discussion about chang<strong>in</strong>g array values <strong>in</strong> a function applies only to<br />

entire arrays that are passed as arguments, and not to <strong>in</strong>dividual elements, whose values<br />

are copied <strong>in</strong>to the correspond<strong>in</strong>g formal parameters and, therefore, cannot be permanently<br />

changed by the function. Chapter 11 discusses this concept <strong>in</strong> greater detail.<br />

Sort<strong>in</strong>g Arrays<br />

To further illustrate the idea that a function can change values <strong>in</strong> an array passed as an<br />

argument, you will develop a function to sort (rank) an array of <strong>in</strong>tegers.The process of<br />

sort<strong>in</strong>g has always received much attention by computer scientists, probably because sort<strong>in</strong>g<br />

is an operation that is so commonly performed. Many sophisticated algorithms have<br />

been developed to sort a set of <strong>in</strong>formation <strong>in</strong> the least amount of time, us<strong>in</strong>g as little of<br />

the computer’s memory as possible. Because the purpose of this book is not to teach<br />

such sophisticated algorithms, you develop a sort function that uses a fairly straightforward<br />

algorithm to sort an array <strong>in</strong>to ascend<strong>in</strong>g order. Sort<strong>in</strong>g an array <strong>in</strong>to ascend<strong>in</strong>g order

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!