Solved

return bool function

Posted on 2007-11-27
13
330 Views
Last Modified: 2010-04-01
what do I return in a bool function?
0
Comment
Question by:Troudeloup
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +4
13 Comments
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 20358262
Hi Troudeloup,

A bool. :)


Sorry.  bool is an invented data type that is really just a short int.  Depending on the implementation, you can return a zero/non-zero value for false/true, or if the compiler enforces strong typing you may have to return a logical value.

  return (answer != 0);    // logical response
  return (answer);          // arithmetic equivalent


Good Luck,
Kent
0
 

Author Comment

by:Troudeloup
ID: 20358272
i don't get it.


so

return;



equals   true?  or false?


how do I return the opposite?
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 20358302
If you want to return true :

        return true;

if you want to return false :

        return false;
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:lucky_james
ID: 20358315
>>return;
its just an empty return.

U should return a bool value as:
return true;
or
return false;
or
bool b
return b;
or
return (a == b) // returns true if a equals b else returns false.
or
return a non-zero value to return true ; a zero for false.

To return the opposite use the 'not' operator ( '!' )
0
 
LVL 6

Expert Comment

by:ashutosh_kumar
ID: 20358319
something like this... if bool gives error use BOOL


bool MyFunc()
{
   bool ReturnValue = false;

  .
  .
  .
  .
  ReturnValue = true;

  return ReturnValue ;
}
0
 
LVL 7

Expert Comment

by:Maverick_Cool
ID: 20358323
return (any poisitive integer >0 say 1); //for true
return (0 ); //for false
0
 
LVL 6

Expert Comment

by:ashutosh_kumar
ID: 20358335
or
bool MyFunc()
{
  .
  .
  return true;
   
}

or

bool MyFunc()
{
  .
  .
  return false;
   
}

0
 
LVL 40

Expert Comment

by:evilrix
ID: 20358398
>> bool is an invented data type that is really just a short int
In C++ bool is a first class type that is normally 1 byte; whereas short int is usually 2 bytes! The point is bool, is bool, it is not int.

bool is a first class type that represents the C++ boolean types true and false (these are NOT the same as the Windows types TRUE and FALSE, which are named int values). In a function that returns bool you should prefer to return a type, value or result of an expression that is semantically boolean.

Examples.
return true;
return false;
return !true;
return !false;
return num != 0;
return num > 0;
return 1 == 1;
return (1 - 2) < 0;

Open in new window

0
 
LVL 40

Expert Comment

by:evilrix
ID: 20358406
Arrrggg -- my examples were lost! Never mind. Answer already accepted.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20358428
>> Arrrggg -- my examples were lost!

Annoying, isn't it ?

        http://www.experts-exchange.com/Community_Support/EE_Bugs/Q_22979641.html
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20358485
Not half as annoying as Troudeloup closing a question within minutes of posting it! I like to make sure the info I am posting is correct so I often test before posting! I've lost track of how many times I've provided him answers only to miss the bar because it's been closed within a few minutes.

Troudeloup, by closing a question (that doesn't have a black or white answer) too quickly you potentially miss out on some good wisdom! Unless there is a specific reason to close it quickly you might want to consider leaving it open for a little longer to give other experts a chance to read the question and construct you an appropriate response! You never know, they might just tell you something important that was missed by the other experts!
0
 

Author Comment

by:Troudeloup
ID: 20358512
I wasn't aware of what I might miss out, thank you for letter me know :D
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20358543
less haste == less waste :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
GUI: DIalog Stacking and Popping in MS C++ 4 96
computer science syllabus 3 115
White board coding practice 3 98
C++ Code Issue 4 48
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question