Furthermore, a function can return a bool value. This can be helpful in several situations. For example, suppose you have a character, and you want to check if it is a vowel. You can use a function called isVowel to determine if it is a vowel. The function would look like this:
- bool isVowel(char ch) {
-
// Perform your tests here
-
// If ch is a vowel,
-
return true;
-
// If it's not a vowel,
-
return false;
-
}
You would call the function like this:
- // code here...
-
if (isVowel(myCharVariable)) { // myCharVariable is a vowel
-
// Do something here
-
} else { // myCharVariable is NOT a vowel
-
// Do something else
-
}
bool returning functions can be very useful in simplifying tests that you will repeat throughout your program.