Sign in to follow this  
sykonet

Question about returning.

Recommended Posts

I was trying to fully understand what return does. I understand that it is supposed to say if the function succeeded or failed. However, whenever I change it to 1 or -1 or anything other than zero, it does nothing. So, I guess I'm asking why exactly you have to do that?

Share this post


Link to post
Share on other sites
Assuming you're talking about one of the C like languages, return values from functions get passed to the function that call them. In the case of main(), the return value gets passed to the process that spawned the application, usually the shell. The return value of main() is often used to signal if the application succeeded. Conventionally a 0 return means it succeeded and everything else means that it failed. For example, compilers return values to show if the compilation succeeded or failed.

Is that what you were asking about?

Share this post


Link to post
Share on other sites
How are you handling the return value? That's most likely your problem. :)

Usually, the return value of a procedure is used to determine wether your procedure ran fine or not. You can check this one of two ways.

if(!myProc(myParams)) { handleErrorHere(); }

If you return 0, then the condition will be true and the code between brackets will be executed. This could be, for instance, code that gracefully informs the user that something went wrong before deinitialising everything and quitting.

int returnValue;
...
returnValue = myProc(myParams);
switch(returnValue)
{
case IS_OK: break;
case DISPLAY_FAILED: ... ;
case CANT_SET_RESOLUTION: ... ;
...
}

This other example checks the value returned in more depth. This is useful if you have multiple error messages and need to handle a failiure on a case-by-case basis.

Share this post


Link to post
Share on other sites
Well, I knew that if it was 0 it meant succeeded, and anything not zero meant failed. For example, I just bought the book, "C++ Primer Fourth Edition" and one of the first exercises it says, to change the return value from 0 to -1 and see how your system treats a failure indicator from main.

And...nothing different happens. So, I was trying to find out why nothing changes, and how is it useful if nothing nothing different happens.

Share this post


Link to post
Share on other sites
Well it makes more of a difference for batch files and scripts. For example, a script can run your program, and if it succeeds, copy a file to another location, or something like that. But generally if you just run something from the command line the return value gets ignored.

Share this post


Link to post
Share on other sites
0 and 1 are just arbitrary values. You can put anything you want in there to indicate failiure/success, but since 0 evaluates to false and anything non-zero evaluates to true, it's easier that way. :)

A script could run your program and do different stuff based on various levels of success (ie, the display initialised fine, but we're gonna have to run in 16 bit color instead of 32; that doesn't warrant a fail but you might still want to keep that in mind.) In the above case, you could just return the bit depth, or a 0 if it failed.

Another, simpler example could be reading a file. If the file was not found or was empty, you'd return 0. Otherwise, you'd return how many lines were read. Since that would be greater than 0, it would evaluate to true and you could later use this value to do stuff with what you've read.

Return values apply to functions more directly than the main, so don't worry too much about the differences if you're not using those yet. :)

Share this post


Link to post
Share on other sites
Quote:
Original post by RuneLancer
Another, simpler example could be reading a file. If the file was not found or was empty, you'd return 0. Otherwise, you'd return how many lines were read. Since that would be greater than 0, it would evaluate to true and you could later use this value to do stuff with what you've read.


Although usually it's the other way around, with 0 indicating success and everything else being failure, isn't it? The reasoning being, if it succeeds, then you don't have to worry, whereas if it fails, you can return a different value depending on what caused the failure. So you could have 1 meaning 'out of memory, 2 meaning 'file not found' and so on.

Share this post


Link to post
Share on other sites
Quote:
Original post by Trapper Zoid
Although usually it's the other way around, with 0 indicating success and everything else being failure, isn't it? (...)


...Yeah, that does make more sense. :) My bad. To boot, that's even how I do things in my game. Hehe. Insert embarassed sheepish grin here.

Share this post


Link to post
Share on other sites
Usually for functions other than main(), it's a better idea stylistically for the return value to be the logical "thing produced by the function". For example:


// A silly function to have; a decent programmer would *probably* never write this
// (although I can think of at least one good reason to, it's not something easily
// explained to a beginner audience), but it makes an easy to understand example.
int sum(int x, int y) {
return x + y;
}

Share this post


Link to post
Share on other sites
I'm sorry, but I don't know if I exactly understand what the question was. If you simply need to test for a value (other than zero) you could just use an if test. Like the following:

if( someFunction != 0 )
{
...
}
else {
...
}

But I may have misunderstood you're original intent on the question. The function you are using, is it one that you wrote, something in the STL, or what? It might be a little easier to help you if we know that!

Usually a function is designed to return values based upon whatever is being done inside of that. So, let's so that, a 0 is returned when a function exitted properly and completed all of its tasks. That means, anything other than zero, would produce an error. But why would it return, let's say, -1 and 1 when they are both errors? They could be error codes that are used internally, sometimes there are a list of return values and what they mean for functions.

It all depends on the function, who wrote it, and what they were using it for. Usually if you google it (as long as its somewhat popular/public) there's a list of error codes and return value meanings. I hope this helped!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this