Predicate Function in C++

Definition: Function that return a boolean(true or false) value is called predicate function.

There are two variations of predication functions:
1. Unary Predicate : Function takes one argument and returns true or false
2. Binary Predicate : Function takes two arguments and returns true or false
Lets take some example to understand this

Code1: Check whether number is even or not

#include <iostream>

using namespace std;

bool isEven(int number)
{
    return ((number%2) == 0);
}

int main()
{
    int num;
    cout << "Enter a number";
    cin >> num;

    if (isEven(num))
    {
        cout << num << " is Even" << endl;
    }
    else{
        cout << num << " is Odd" << endl;
    }
    return 0;
}

Code2: Check whether character is vowel or not

#include <iostream>

using namespace std;

bool isEven(int number)
{
    return ((number%2) == 0);
}

bool isVowel(char ch)
{
    if ((ch == 'A') || (ch == 'a') || (ch == 'E') || (ch == 'e')||
        (ch == 'I') || (ch == 'i') || (ch == 'O') || (ch == 'o')||
        (ch == 'U') || (ch == 'u'))
            return true;
    else
        return false;
}


int main()
{
    char letter;
    cout << "Enter a letter";
    cin >> letter;

    if (isVowel(letter))
    {
        cout << letter << " is Vowel" << endl;
    }
    else{
        cout << letter << " is Consonant" << endl;
    }
    return 0;
}

Comments

Popular posts from this blog

bb.utils.contains yocto

Difference between RDEPENDS and DEPENDS in Yocto

make config vs oldconfig vs defconfig vs menuconfig vs savedefconfig