### Can I check that the function worked?

Hello
When I code a program, sometimes I confront this problem.

Psudocode is this.

 ``1234567`` ``````void FuncA() {blah blah...} void FuncB() {blah blah...} void main() { if(FuncA() is worked) make FuncB() work; }``````

My question is this.
'FuncA() is worked' is can be typed boolean?

Last edited on
Sure, just have FuncA return a `bool`.

 ``1234`` ``````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 */ }`
FuncA() can return a bool or any integral type (that is, int, long, short, ...). C treats zero as false, and non-zero as true.
Last edited on

but, in case that FuncA, FuncB need to return the values[int, double, string... whatever],
then this way can't be used, right?

For example...
 ``12345678910`` ``````int FuncA(int x) { int valueA = (x)*(x); return valueA; } int FuncB(int x) { int valueB = (x)*(x)*(x); return valueB; }``````
Well, there are still a bunch of ways you could check return types or set status flags.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950`` ``````#include #include // 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; return true; } // 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; }``````
Ah, thx again iHutch105

In this case, what if the functions are not user-definition, but built-in functions?

Like,`sqrt()` (um, I can't give an another example right now...) and so on...
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
Last edited on
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.

[1] http://www.cplusplus.com/reference/cmath/sqrt/
Last edited on
You could also ` throw ` if the function encountered a problemo.
Topic archived. No new replies allowed.