bool FuncA()
{
// Return true or false for success/fail
}
Then you can assess the return value in an if statement. if( FuncA() ) { /* FuncA returned true */ }
or... if( !FuncA() ) { /* FuncA returned false */ }
#include <iostream>
#include <string>
// Return an integer
int FuncA( int n )
{
return n * n;
}
// Return a string
std::string FuncB()
{
return"Yay!";
}
// Return a bool, take integer reference
bool FuncC( int &ret )
{
ret *= ret;
returntrue;
}
// Return an int, take int and bool reference
int FuncD( int n, bool &status )
{
status = true;
return n * n;
}
int main( int argc, char *argv[] )
{
int a = 10;
bool status = false;
if( FuncA( a ) > 0 )
std::cout << "FuncA returned greater than zero\n";
if( FuncB() == "Yay!" )
std::cout << "FuncB returned \"Yay\"\n";
if( FuncC( a ) )
std::cout << "FuncC returned true, a is " << a << std::endl;
a = FuncD( a, status );
if( status )
std::cout << "FuncD set status to true, a is " << a << std::endl;
return 0;
}
First ask yourself what you exactly mean by FuncA() is worked
Then check for this condition... ;)
---
a] realize that there are number of functions that simply cannot "not work"
- then you may be interested in the result rather than actual functionality
b] those function that can "not work" usually either
- return bool
- return an errorcode integer
- set up a global errorcode indicator (deprecated - not thread safe bhv )
- throw exception
b.1] If it is your choice, choose any
b.2] If somebody else did, read documentation
Built in functions will have their own prototypes. It really depends on what they do already.
For example, sqrt can only return whatever the prototype defines; a double, float or whatever.
If you take a look at the reference[1] for it you can see how it handles what it considers would be failures (for example, passing in a negative value). In the case of sqrt, the global errno variable is set to EDOM to flag a domain error.