12.12.2012 Views

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

NOTE<br />

Wad<strong>in</strong>g In Deeper<br />

The displayRecord() function, which beg<strong>in</strong>s on l<strong>in</strong>e 76, takes two parameters. The first<br />

parameter, num, is an <strong>in</strong>t that conta<strong>in</strong>s the <strong>in</strong>dex number of the record to display. This variable<br />

is used only to display the record number. On l<strong>in</strong>e 78 I add 1 to num when I display it because<br />

users are accustomed to lists beg<strong>in</strong>n<strong>in</strong>g with 1 rather than with 0. (I aim to please!) The second<br />

parameter of the displayRecord() function is an <strong>in</strong>stance of the mail<strong>in</strong>gListRecord structure.<br />

Inside the displayRecord() function I use the local <strong>in</strong>stance of the structure passed <strong>in</strong><br />

(which represents a copy of the structure) to display the contents of the structure.<br />

In this case I am pass<strong>in</strong>g the mail<strong>in</strong>gListRecord structure by value.<br />

What this means is that a copy of the structure is created each time the<br />

displayRecord() function is called. This is not very efficient because of<br />

the overhead required to pass a structure by value. The overhead comes<br />

<strong>in</strong> the form of the extra time and memory required to make a copy of<br />

the structure each time the function is called. It would be better to<br />

pass the structure by reference, but I haven’t talked about that yet,<br />

because the structure is passed by value <strong>in</strong> this program. You will<br />

learn about pass<strong>in</strong>g by reference tomorrow when we discuss functions<br />

<strong>in</strong> <strong>C++</strong>.<br />

Note that the displayRecord() function is called from both the for loop when all the records<br />

are displayed (l<strong>in</strong>e 49) and aga<strong>in</strong> from the ma<strong>in</strong> body of the program to display the actual<br />

record chosen (l<strong>in</strong>e 72). That’s precisely why the code to display a record has been placed <strong>in</strong><br />

a function. By putt<strong>in</strong>g it <strong>in</strong> a function, I only have to write the code once and can avoid<br />

duplicat<strong>in</strong>g the code unnecessarily.<br />

TIP<br />

Any time you f<strong>in</strong>d yourself repeat<strong>in</strong>g code more than a couple times <strong>in</strong><br />

your programs, th<strong>in</strong>k about mov<strong>in</strong>g that code to a function. Then you<br />

can call the function when you need that code executed.<br />

There is another segment of this program that deserves mention. Look at this do-while loop,<br />

which beg<strong>in</strong>s on l<strong>in</strong>e 59:<br />

do {<br />

rec = getch();<br />

rec -= 49;<br />

} while (rec < 0 || rec > 2);<br />

This code first gets a character from the keyboard us<strong>in</strong>g the getch() function. As you have<br />

63<br />

2

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

Saved successfully!

Ooh no, something went wrong!