Like Tree4Likes
  • 4 Post By grumpy

A function that returns a bool if true.

This is a discussion on A function that returns a bool if true. within the C++ Programming forums, part of the General Programming Boards category; I have to write function that returns a bool that is true if it meets a certain condition, otherwise it ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    1

    A function that returns a bool if true.

    I have to write function that returns a bool that is true if it meets a certain condition, otherwise it should return false. I don't know what this means. I have never heard of the word "bool".

    Would it be something like this?

    Code:
    if(... == 0)
            return true;
        else
            return false;
    }

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    A bool is something that is either true or false.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,283
    bool is short for boolean, and is a data type that has two values - true and false. If a boolean value is not true, then it is false.

    Let's say we're writing a function that accepts an int argument, and the "certain condition" being tested is for that argument to be equal to 2. Then one way of doing it is;
    Code:
    bool test_A(int x)
    {
         if (x == 2)
             return true;
         else
             return false;
         
    }
    This can be simplified to ....
    Code:
    bool test_B(int x)
    {
         if (x == 2)
             return true;
         return false;
    }
    (i.e. the else clause is eliminated) since the only way to return false is if x is not equal to 2. (Note this should not necessarily be done for functions that need to do other work before returning.

    Now, the thing is, x == 2 is also a boolean test (it yields true or false, depending on the value of x). So this code can be simplified even further
    Code:
    bool test_C(int x)
    {
         return (x == 2) ? true : false;
    }
    or even to
    Code:
    bool test_D(int x)
    {
         return (x == 2);
    }
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bool function always seems to return true
    By The Doctor in forum C++ Programming
    Replies: 1
    Last Post: 10-22-2012, 11:07 PM
  2. Replies: 4
    Last Post: 10-03-2011, 06:30 AM
  3. Why the eof returns false when it should return true?
    By kulfon in forum C++ Programming
    Replies: 4
    Last Post: 02-07-2011, 08:57 AM
  4. Finding a key in a map returns true always
    By Zach_the_Lizard in forum C++ Programming
    Replies: 2
    Last Post: 09-10-2009, 05:33 PM
  5. write a function that returns true
    By tingting in forum C Programming
    Replies: 8
    Last Post: 08-10-2009, 09:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21