4.15. Bool Functions¶
Functions can return bool values just like any other type, which is often convenient for hiding complicated tests inside functions. For example:
bool is_digit (int x) {
if (x >= 0 && x < 10) {
return true;
}
return false;
}
The name of this function is is_digit
. It is common to give boolean
functions names that sound like yes/no questions. The return type is
bool, which means that every return statement has to provide a bool
expression.
The code itself is straightforward, although it is a bit longer than it needs to be. Remember that the expression ‘’x >= 0 && x < 10’’ has type bool, so there is nothing wrong with returning it directly, and avoiding the if statement altogether:
bool is_digit (int x) {
return (x >= 0 && x < 10);
}
In main you can call this function in the usual ways:
cout << is_digit (2) << endl;
bool is_big = !is_digit (17);
The first line outputs the value true because 2 is a single-digit number. Unfortunately, when C++ outputs bools, it does not display the words true and false, but rather the integers 1 and 0.
The second line assigns the value true to is_big
only if 17 is not a
single-digit number.
The most common use of bool functions is inside conditional statements
if (is_digit (x)) {
cout << "x is little\n";
}
else {
cout << "x is big\n";
}
-
Q-2: Match the conditional statement to its output, assuming it is outputted using cout and x = 3.
Try again!
- (x%2 == 1 && x == 7)
- 0
- (x%2 == 0 || x + 1 == 4)
- 1
Construct a block of code that first checks if a number is positive, then checks if it’s even, and then prints out a message to classify the number. It prints “both” if the number is both positive and even, “even” if the number is only even, and finally “positive” if the number is only positive.