Search code examples
booleanboolean-operations

return type of bool function


what is the return type of this sort of bool function...... i know the return type is either true of false but this seems complicated when you have got like this..

bool mypredicate (int i, int j) {
return (i==j);
}

this bool function are used in a library function called equal...... another example is....

bool compare(int a, int b){
return a<b;
}

so what are the perspective here to return type of these bool function.when is goes true & false....


Solution

  • Your functions mypredicate and compare are merely thin wrappers over the binary operators == and <. Operators are like functions: they take a number of arguments of a given type, and return a result of a given type.

    For example, imagine a function bool operator==(int a, int b) with the following specification:

    • if a equals b then return true
    • otherwise return false

    And a function bool operator<(int a, int b) with the following specification:

    • if a is strictly lesser than b then return true
    • otherwise return false.

    Then you could write:

    bool mypredicate (int i, int j) {
        return operator==(i, j);
    }
    
    bool compare(int a, int b){
        return operator<(a, b);
    }
    

    For convenience, most programming languages allow you to use a shorter, functionnaly equivalent syntax: i == j and a < b.