There is an occasional need for a function which returns a "true" or "false". For example, "while (UserWantsToContinue())..." or "if (IsValid(choice))...".
The data type "bool" is compiler-dependent. Older versions of Turbo C++ and Borland C++ do not support any Boolean data type. Version 4.0 of Borland C++ supports the "bool" type, but it requires that you include bool.h library. Version 5.0 of Borland C++ (and later versions) allow you to use "bool" as a data type without requiring you to include bool.h. On some compilers, the data type itself might be named "boolean" instead of "bool". The ANSI C++ standard now includes the built-in data type bool.
The Boolean data type--if it is supported by a particular compiler--has two values: true (stored as a 1) and false (stored as a 0). The values themselves ("true" and "false") may be represented in some version of C++ as true and false and in others as TRUE and FALSE. The name of the type may vary from compiler to compiler. Your instructor has seen Boolean, bool, and Bool offered by three different compilers.
Before the bool type was included in the C++ standard, there were good reasons for not using a built-in Boolean data type--namely, it makes your programs less "portable". However, if you choose to use a Boolean data type, it is your responsibility as a programmer to find if the Boolean data type is built-into the particular version of the language which you are using, and, if so, how to use it. What is it named? Must you include a header file in order to use it? Are the value true and false or TRUE and FALSE? If you use a non-standard data type in C++, it is nice to place a comment at the top of the program--for readers who later wish to modify the program and/or port it to a different system--indicating that you are using a non-standard subroutine in the program.
The following 3 examples accomplish the same task. The first is quite portable because it returns an integer (1 for true, 0 for false) and doesn't rely on any built-in data types; the 2nd assumes the existence of a Boolean data type bool and, therefore, relies on the existence of that type if the program is modified in the future using a different compiler; and the 3rd has a "programmer-defined data type" Boolean which allows the programmer to use the values TRUE and FALSE in an intuitive way. The 2nd and 3rd cases are the most readable. The first and 3rd cases are the most portable.
Case One: No Boolean data type. (GOOD way to go for portability purposes!)
// prototype
int IsValid (char);
...
// Get user's choice (Assume the only valid choices are 'A' and 'B'.)
cout << "Choice? ";
cin >> choice;
if (IsValid(choice) )
ProcessChoice(choice);
else
PrintErrorMsg();
...
// Declaration of the function
int IsValid (char choice)
{
int signal;
if (choice == 'A' || choice == 'B')
// where "||" is the "OR" operator
signal
= 1;
else
signal
= 0;
return signal;
}
Case Two: Support for Boolean data type called "bool".
Note: Some compilers might require that you include bool.h (#include <bool.h>). Borland C++( vers 5.0 and later) does not require this.
// prototype
bool IsValid (char);
...
// Get user's choice (Assume the only valid choices are 'A' and 'B'.)
cout << "Choice? ";
cin >> choice;
if (IsValid(choice) )
ProcessChoice(choice);
else
PrintErrorMsg();
...
// Declaration of the function
bool IsValid (char choice)
{
bool signal;
if (choice == 'A' || choice == 'B')
// where "||" is the "OR" operator
signal
= true;
else
signal
= false;
return signal;
}
Case Three: (As discussed in Chapter Eight of your
text)
Using a programmer-defined Boolean data type called
"Boolean".
// Get user's choice (Assume the only valid choices are 'A' and 'B'.)
cout << "Choice? ";
cin >> choice;
if (IsValid(choice) )
<-- Notice that this chunk of code is the same for all 3 cases!
ProcessChoice(choice);
else
PrintErrorMsg();
...
// Declaration of the function
Boolean IsValid (char choice)
{
Boolean signal;
if (choice == 'A' || choice == 'B')
// where "||" is the "OR" operator
signal = TRUE;
else
signal
= FALSE;
return signal;
}
For further examples, see Nim.CPP in the Demos folder on the K drive. It contains two Boolean functions, both of which return values of type bool:
IsValid(...) returns a true if the character typed in (for the number of sticks to be removed) is a digit (and not some other character) and there are enough sticks left.
MORAL OF THE STORY:
Dealing with true or false values is now standard in C++, but that doesn't mean that you'll always be using a compiler based upon that standard. It is nice to know how to create your own Boolean data type (case #3 above) in case you'll need it some day.
In general, avoid "bells & whistles" which come with a particular version of a language unless you are sure that the program will never need to be "ported to" another system. When you do use "bells & whistles", notify the reader by including a comment at the top of the program, giving the reader a list of non-standard types and subroutines which the program uses, as in --