Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

C Programming for the Absolute Beginner phần 5 potx
Nội dung xem thử
Mô tả chi tiết
a function that contains the logic and structures to handle this procedure and then reuse
that function when needed. Putting all the code into one function that can be called repeatedly will save you programming time immediately and in the future if changes to the function
need to be made.
Let me discuss another example using the printf() function (which you are already familiar
with) that demonstrates code reuse. In this example, a programmer has already implemented
the code and structures needed to print plain text to standard output. You simply use the
printf() function by calling its name and passing the desired characters to it. Because the
printf() function exists in a module or library, you can call it repeatedly without knowing
its implementation details, or, in other words, how it was built. Code reuse is truly a programmer’s best friend!
Information Hiding
Information hiding is a conceptual process by which programmers conceal implementation
details into functions. Functions can be seen as black boxes. A black box is simply a component, logical or physical, that performs a task. You don't know how the black box performs
(implements) the task; you just simply know it works when needed. Figure 5.2 depicts the
black box concept.
FIGURE 5.2
Demonstrating
the black box
concept.
Consider the two black box drawings in Figure 5.2. Each black box describes one component;
in this case the components are printf() and scanf(). The reason that I consider the two
functions printf() and scanf() black boxes is because you do not need to know what’s inside
of them (how they are made), you only need to know what they take as input and what they
return as output. In other words, understanding how to use a function while not knowing
how it is built is a good example of information hiding.
Many of the functions you have used so far demonstrate the usefulness of information hiding.
Table 5.1 lists more common library functions that implement information hiding in structured programming.
Chapter 5 • Structured Programming 113
If you’re still put off by the notion of information hiding or black boxes, consider the following
question. Do most people know how a car’s engine works? Probably not, most people are only
concerned that they know how to operate a car. Fortunately, modern cars provide an interface
from which you can easily use the car, while hiding its implementation details. In other
words, one might consider the car's engine the black box. You only know what the black box
takes as input (gas) and what it gives as output (motion).
Going back to the printf() function, what do you really know about it? You know that the
printf() function prints characters you supply to the computer’s screen. But do you know
how the printf() function really works? Probably not, and you don’t need to. That’s a key
concept of information hiding.
In structured programming you build components that can be reused (code reusability) and
that include an interface that other programmers will know how to use without needing to
understand how they were built (information hiding).
FUNCTION PROTOTYPES
Function prototypes tell C how your function will be built and used. It is a common programming practice to construct your function prototype before the actual function is built. That
statement was so important it is worth noting again. It is common programming practice
to construct your function prototype before the actual function is built.
Programmers must think about the desired purpose of the function, how it will receive
input, and how and what it will return. To demonstrate, take a look at the following function
prototype.
TABLE 5.1 C OMMON LIBRARY F UNCTIONS
Library Name Function Name Description
Standard input/output scanf() Reads data from the keyboard
Standard input/output printf() Prints data to the computer monitor
Character handling isdigit() Tests for decimal digit characters
Character handling islower() Tests for lowercase letters
Character handling isupper() Tests for uppercase letters
Character handling tolower() Converts character to lowercase
Character handling toupper() Converts character to uppercase
Mathematics exp() Computes the exponential
Mathematics pow() Computes a number raised to a power
Mathematics sqrt() Computes the square root
114 C Programming for the Absolute Beginner, Second Edition