I have senn that it is possible to make function return a nummber.
Like 1 for TRUE or 0 for FALSE. How to do that.
Printable View
I have senn that it is possible to make function return a nummber.
Like 1 for TRUE or 0 for FALSE. How to do that.
yes:
return 0;
or
return 1;
Note that the most recent ANSI C standard has standard boolean-type which you can use when including stdbool.h. I don't know if it is in the ANSI C++ standard.Code:// For improving code readability constants are recommended
typedef enum
{
FALSE,
TRUE
} boolean;
boolean function ()
{
boolean retval;
....
// Now return the value
return retval;
}
you can just go:
Code:BOOL function()
{
return TRUE;
// OR return FALSE;
}
I thought bool was in the standard so there is no need for enums, typdefs, #defines, consts or anything else I cant think of. You can usually just do this:
Code:bool someFunction( bool flag )
{
return (flag ? true : false);
}
Hope this helps too:
Hope i didn't mess up somewhere. Did it at work.... :D with no compilerCode:
#include <iostream.h>
bool something(int);
void main(void)
{
int testing;
bool answer;
cout << "Enter a number (other than zero) \n";
cout << "and i'll tell you if it positive or negative: ";
cin >> testing;
answer = something(testing); // 4. you need to capture the return from the something function.
if (answer)
{
cout << "Your input was a positive number.\n";
}
else
{
cout << "Your input was a negative number.\n";
}
}
bool something(int num) // 1. you need (bool) here to tell the function it will return one.
{
if (num > 0)
{
return true; // 2. this will return true(1) if the input is greater than zero
}
else
return false; // 3. this will return false(0) if the input is less than zero
}
Oh and one more thing. I love this place and I learned a lot here just by reading. You could too if you just ask, but If you are planning on asking, you should at least register your name. I would hate to think of this board like a place that people come to get help but don't even care enough to let us know who we are helping. Sorry, i'm a beginner too just like you and it just doesn't look right when there a whole bunch of "Unregistered" names out there.
.... Sorry for my blabering ..... i'll go back to work now ... :D
Keep up the programming.
>void main(void)
Ack! Call Prelude!
Use:
int main (void) {
//blablabal code blablabla
return 0;
}