Programming in C

(Barry) #1

152 Chapter 8 Working with Functions


you get an error from the compiler because it doesn’t know about nRowsand nCols
when it sees them listed in the declaration of matrix.
As you can see, the output shown in Program 8.13A matches that shown in Program
8.13. Now, you have two functions (scalarMultiplyand displayMatrix) that you can
use with matrices of any size.This is one of the advantages of using variable-length
arrays.^3

Global Variables


It is now time to tie together many of the principles you have learned in this chapter, as
well as learn some new ones.Take Program 7.7, which converted a positive integer to
another base, and rewrite it in function form.To do this, you must conceptually divide
the program into logical segments. If you glance back at that program, you see that this is
readily accomplished simply by looking at the three comment statements inside main.
They suggest the three primary functions that the program is performing: getting the
number and base from the user, converting the number to the desired base, and display-
ing the results.
You can define three functions to perform an analogous task.The first function you
call is getNumberAndBase.This function prompts the user to enter the number to be
converted and the base, and reads these values in from the terminal. Here, you make a
slight improvement over what was done in Program 7.7. If the user types in a value of
base that is less than 2 or greater than 16, the program displays an appropriate message at
the terminal and sets the value of the base to 10. In this manner, the program ends up
redisplaying the original number to the user. (Another approach might be to let the user
reenter a new value for the base, but this is left as an exercise.)
The second function you call is convertNumber.This function takes the value as
typed in by the user and converts it to the desired base, storing the digits resulting from
the conversion process inside the convertedNumberarray.
The third and final function you call is displayConvertedNumber.This function takes
the digits contained inside the convertedNumberarray and displays them to the user in
the correct order. For each digit to be displayed, a lookup is made inside the baseDigits
array so that the correct character is displayed for the corresponding digit.
The three functions that you define communicate with each other by means of global
var iables. As noted previously, one of the fundamental properties of a local variable is that
its value can be accessed only by the function in which the variable is defined. As you
might expect, this restriction does not apply to global variables.That is, a global variable’s
value can be accessed by anyfunction in the program.
The distinguishing quality of a global variable declaration versus a local variable dec-
laration is that the former is made outsideof any function.This indicates its global
nature—it does not belong to any particular function.Anyfunction in the program can
then access the value of that variable and can change its value if desired.


  1. As noted earlier, just make certain your particular C compiler offers full support for variable-
    length arrays so that you can use this feature.

Free download pdf