Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Doubts with return(value)

Status
Not open for further replies.

cadbilbao

Programmer
Apr 9, 2001
233
ES
Hi!

I'm developing a program, formed by several subrutines.

If the returned value from program is zero, proccess went OK. I want to return program errors from subrutines inside program, but these 'return's are only for the subrutines, not for my program.

**I want to know the way to return program errors from the subrutines inside this prgram***.

Thank you very much.
 
You have a few options. It depends whether your functions return some value. If not you can just return say a bool true or false to indicate that all is OK (or not).

bool myfunc(int i)
{
if (error) return false;
return true;
}

if (myfunc(21))
{
// Call OK
}

If they do, is there an "invalid value" you can return when something goes wrong. If so check for this value after calling the routine. However if a routine returns an int for example and any int is valid there is no way to indicate an error. Then you have two options: either return your value with a reference parameter (or pointer) and have the return value a bool:

bool myfunc(int& i)
{
if (error) return false;

i = someresult;
return true;
}

int k = 21;
if (myfunc(k))
{
// Call OK k = result
}

Or use exceptions (really the recommended method anyway).
 
You could also use exception handling with try catch statements.

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top