|
|
|
|
|
|
|
(text box continued from previous page) |
|
|
|
|
|
|
|
|
status = remove(junkfile.dat);
if (status != 0)
PrintErrorMsg(); |
|
|
|
| |
|
|
|
|
On the other hand, if you assume that the system always succeeds at deleting a file, you can ignore the returned status by calling remove as though it were a void function: |
|
|
|
| | |
|
|
|
|
The remove function is sort of a hybrid between a void function and a valuereturning function. Conceptually, it is a void function; its principal purpose is to delete a file, not to compute a value to be returned. Literally, however, it's a value-returning function. It does return a function valuethe status of the operation (which you can choose to ignore). |
|
|
|
| |
|
|
|
|
In this book, we don't write hybrid functions. We prefer to keep the concept of void function distinct from value-returning function. But there are two reasons why every C++programmer should know about the topic of ignoring a function value. First, if you accidentally call a value-returning function as if it were a void function, the compiler won't prevent you from making the mistake. Second, you sometimes encounter this style of coding in other people's programs and in the C++ standard library. Several of the library functions are technically value-returning functions, but the function value is used merely to return something of secondary importance like a status value. |
|
|
|
|
|
|
|
|
|
function. (We should point out that not everyone agrees with this point of view. Some programmers feel that performing I/O within a value-returning function is perfectly acceptable. You will find strong opinions on both sides of this issue.) |
|
|
|
|
|
|
|
|
There is another advantage to using only value parameters in a value-returning function definition: you can use constants and expressions as actual parameters. For example, we can call the IsTriangle function in the following manner using literals and an expression: |
|
|
|
|
|
|
|
|
if (IsTriangle(30.0, 60.0, 30.0 + 60.0))
cout << A 306090 angle combination forms a triangle.;
else
cout << Something is wrong.; |
|
|
|
|
|