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,274
    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,671
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

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-23-2012, 12:07 AM
  2. Replies: 4
    Last Post: 10-03-2011, 07: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, 09: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, 06:33 PM
  5. write a function that returns true
    By tingting in forum C Programming
    Replies: 8
    Last Post: 08-10-2009, 10:15 PM

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