Can I check that the function worked?

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

Psudocode is this.

1
2
3
4
5
6
7
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?

Thx for reading! :)
Last edited on
Sure, just have FuncA return a bool.

1
2
3
4
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
Thx for answer! ^^

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...
1
2
3
4
5
6
7
8
9
10
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#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;
  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.