Section 7d

Boolean Functions


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Functional Abstraction - Using a Boolean Example

Handy Tools

Boolean functions can be powerful tools to help you make decisions but to keep the messy stuff out of the way of the problem-solving activities. All you need to do to develop and use Boolean functions is to remember that various functions can return various values and data types, so it stands to reason that Boolean functions can return Boolean values. Suppose you had a function that implemented the test for leap year. The following is an example of how this function might be implemented.

bool isLeapYear( int year )
{
bool fourthYear, notACentury, fourthCentury;

fourthYear = ( year % 4 == 0 );

notACentury = ( year % 100 != 0 );

fourthCentury = ( year % 400 == 0 );

return( fourthYear && notACentury || fourthCentury );
}

Recall from your previous work that you can return a value or a calculation back from a called function, so it should be logical that you can return a Boolean expression. The above is a good example of a Boolean function. Note that from the code in the previous topic, it could have been narrowed down to the simple Boolean expression [i.e., return ( year % 4 == 0 ) && ( year % 100 != 0 ) || ( year % 400 == 0 ); ], but that is not readable or clear, and would be very difficult to debug if someone unfamiliar with the algorithm were assigned to work on this function.

Now, if you can return Boolean expressions in the form of functions, then it is logical that you can test them just as you would a Boolean expression . . . and you can. Consider the following.

if( isLeapYear( presentYear ) == true )
{
daysInFebruary = 29;
}

The above is a good example of how you can use abstraction to present a simple test. The messy part of all the leap year Boolean processing is hidden inside the function, but it is clear what you are testing in your code. The nice thing about this is that it can be made even easier.

However, remember from the previous topic that the computer only cares about the result of Boolean expressions. It makes its decisions from the true or false value that is found between the parentheses. Above, the if statement is looking to see if isLeapYear( presentYear ) is in fact equal to true. However, if the function can actually be equal to true, then by itself it can be tested for "truth". Consider the following.

if( isLeapYear( presentYear ) )
{
daysInMonth = 29;
}

Think about this carefully. What does the computer want to see between the parentheses? It wants to see something that evaluates to true or false. The isLeapYear function can do this. Therefore, it is redundant to test for its equality to true or false. While it is legal and appropriate code, it is not necessary. In most conditions where Boolean functions are used, they are used as you see in the example above [i.e., if( isLeapYear( presentYear ) ) ]. Plan on using this form of code as you learn more about using functions.

Consider these last two examples from a mouse in a maze program that tests for possible movement. To test to see what is ahead before moving, you might write one of the following if statements, using example Boolean functions as shown.

if( doesNotHaveWallToLeft() )

if( doesNotHaveWallAhead() )

These two functions are tested for "truth". They could have been presented in the form if( doesNotHaveWallAhead() == true ), but again, that would have been redundant. The form you see here is easier to read, and is actually more like the English question, "If the mouse does not have a wall ahead of him" . . . take the next action.

There is not much more to say about Boolean functions. The great value of them is that they abstract away the details of certain tests so that the programmer, and the person reading the code later on, can focus on the problem at hand rather than worrying about how a certain test might work. Using Boolean functions will make your design operations easier, and your resulting code cleaner and better.

Watch this video to see the isLeapYear function go together and get used.