Programming in C

(Barry) #1

140 Chapter 8 Working with Functions


The first thing that catches your eye inside mainis the prototype declaration for the
minimumfunction.This tells the compiler that minimumreturns an intand takes an array
of 10 integers. Remember, it’s not necessary to make this declaration here because the
minimumfunction is defined before it’s called from inside main.However, play it safe
throughout the rest of this text and declare all functions that are used.
After the array scoresis defined, the user is prompted to enter 10 values.The scanf
call places each number as it is keyed in into scores[i], where iranges from 0 through
9. After all the values have been entered, the minimumfunction is called with the array
scoresas an argument.
The formal parameter name valuesis used to reference the elements of the array
inside the function. It is declared to be an array of 10 integer values.The local variable
minValueis used to store the minimum value in the array and is initially set to
values[0], the first value in the array.The forloop sequences through the remaining
elements of the array, comparing each element in turn against the value of minValue.If
the value of values[i]is less than minValue,a new minimum in the array has been
found. In such a case, the value of minValueis reassigned to this new minimum value
and the scan through the array continues.
When the forloop has completed execution,minValueis returned to the calling
routine, where it is assigned to the variable minScoreand is then displayed.
With your general-purpose minimumfunction in hand, you can use it to find the
minimum of anyarray containing 10 integers. If you had five different arrays containing
10 integers each, you could simply call the minimumfunction five separate times to find
the minimum value of each array. In addition, you can just as easily define other func-
tions to perform tasks, such as finding the maximum value, the median value, the mean
(average) value, and so on.
By defining small, independent functions that perform well-defined tasks, you can
build upon these functions to accomplish more sophisticated tasks and also make use of
them for other related programming applications. For example, you could define a func-
tion statistics, which takes an array as an argument and perhaps, in turn, calls a mean
function, a standardDeviationfunction, and so on, to accumulate statistics about an
array.This type of program methodology is the key to the development of programs that
are easy to write, understand, modify, and maintain.
Of course, your general-purpose minimumfunction is not so general purpose in the
sense that it only works on an array of precisely 10 elements. But this problem is rela-
tively easy to rectify.You can extend the versatility of this function by having it take the
number of elements in the array as an argument. In the function declaration, you can
then omit the specification of the number of elements contained in the formal parame-
ter array.The C compiler actually ignores this part of the declaration anyway; all the
compiler is concerned with is the fact that an array is expected as an argument to the
function and not how many elements are in it.
Program 8.10 is a revised version of Program 8.9 in which the minimumfunction
finds the minimum value in an integer array of arbitrary length.
Free download pdf