Question about returning.

Started by
9 comments, last by bkt 18 years, 9 months ago
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?
Advertisement
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?
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.
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.
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.
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. :)
Ah, I see now, thanks to the both of you!
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.

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.
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;}

This topic is closed to new replies.

Advertisement