Given a function that returns a value, is it possible to exit the function given a certain condition without returning anything? If so, how can you accomplish this?
Example:
int getNumber ()
{
. . .
}
So say you are in this function. Is there a way to exit it without it doing anything?
A function can't break on behalf of its caller. The break has to be syntactically inside the loop. Show activity on this post. You want to use return , not break .
When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop. It can be used to terminate a case in the switch statement (covered in the next chapter).
To break out of a function in Python, we can use the return statement. The Python return statement can be very useful for controlling the flow of data in our Python code.
exit function h>, terminates a C++ program. The value supplied as an argument to exit is returned to the operating system as the program's return code or exit code. By convention, a return code of zero means that the program completed successfully.
Alternative of exceptions is return of status code:
SomeStatusCode YourFunc(int &returnValue)
{
...
returnValue = SomeValue;
return SomeStatusCode.Successful;
...
return SomeStatusCode.Fail;
}
//in main func
if(YourFunc(retValue)==SomeStatusCode.Successful)
// work with retValue
else
// nothing to do, show error, etc.
</code>
You have two options: return
something or throw
.
int getNumber()
{
return 3;
}
int getNumber()
{
throw string("Some Var");
}
If you throw
, you have to catch
the type you threw.
int maint(int argc, char ** argc)
{
try
{
getNumber();
}
catch(string std)
{
//Your code will execute here if you throw
}
}
You might get away with a return;
with no return value. The compiler will warn, but may not error.
BUT, why on earth would you even consider this? What are you trying to achieve?
And what does the calling code look like? If it is int someVar = getNumber();
then the value of someVar is undefined (which is A Bad Thing).
It sounds to me like you want to return two pieces of information, one of which tells you if the other is valid.
Try something like
bool GetNumber(int * outputNumber)
{
if ....
{
*outputNumber = ...;
return true;
}
else
{
return false; // contents of outputNumber are undefined
}
}
int number;
bool isNumberValid;
isNumberValid = GetNumber(&number);
if (isNumberValid)
... do something with number
Simple solution:
boost::optional<int> getNumber( )
{
// May now return an int, or may return nothing.
}
For float and double there is an alternative but not for int types.
#include <limits>
double Get()
{
// no valid return value
return std::numeric_limits<double>::quiet_NaN();
}
double val = Get();
if(val != val) {
// Retrieved no valid return value
}
else {
// No NaN retrieved
}
Be careful when using NaN. Each condition on val will be true.
Make it a requirement for the user to check first that the queue is not empty (provide means for that).
Then you can:
Checking and then invoking undefined behavior (returning an arbitrary value) is the worst thing to do. You get both the runtime overhead of the check and the caller is no wiser, as the result is still undefined.
You could throw a specified exception and catch it accordingly.
You could also return a class/struct with some magic conversion that is useful for your special need.
Real-life example: once I had actually to return two pieces of information in the same return value, a value to report if a window message had been completely processed and the value to be returned for it if the processing was already completed. So I packed everything up in a class like this:
//This class is used to carry a return value for a window message and a value that indicates if the
//message has already been completely processed
class MessageReturnValue
{
public:
LRESULT returnValue;
bool processed;
MessageReturnValue()
{
processed=false;
returnValue=FALSE;
};
MessageReturnValue(LRESULT ReturnValue)
{
processed=true;
returnValue=ReturnValue;
};
MessageReturnValue(bool Processed)
{
returnValue=FALSE;
processed=Processed;
};
inline operator bool()
{
return processed;
};
inline operator LRESULT()
{
return returnValue;
};
};
This allowed me to do just return false;
in a function that returned a MessageReturnValue if the message had still to be processed, or to return TRUE/15/whatever;
if the message had been completely processed and I already had a return value. The caller, on its side, could simply do
LRESULT MyWndProc(/* blah blah blah */)
{
MessageReturnValue ret = MyFunction(/* blah blah blah */);
if(!ret)
{
/* process the message */
return /* something */;
}
else
return (LRESULT)ret;
}
If you have more complicated needs, you could also consider using boost::tuple.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With