Sign in to follow this  
ItsNotATumaaa

Another newbie question,

Recommended Posts

Sorry for all the n00b questions, I don't mean to seem annoying I just suck at teaching myself. Anyway, I was curious as to the purpose of returning a value after each function. What is its purpose? What does it accomplish? What does it mean when it returns? Thank you.

Share this post


Link to post
Share on other sites
You return these values for error checking.

return a positive number for the function being succesful and a negative one it not. When you call the function you test the return value to make sure the function did what you wanted it to.

This was the only way of catching errors in the past. Now we have throw/try/catch. Returning values is not as important now.

theTroll

Share this post


Link to post
Share on other sites
Greetings, ItsNotATumaaa!

I dont know if you're referring to return values for specific functions, or just any functions. But in addition to Troll's reason for returning values (error checking), there is the obvious reason - because you need to know the result of a function.

Functions generally have, or do, 3 things. Input, computation, and output. The input is what you tell it to use for its "computations," computation are whatever calculations this function does as its purpose for existing, and the output is the result of its computations. As in the case Troll mentioned, not all functions have "output." In this case, the return value is just the success or failure of the computation. But whenever a function DOES have a result/output, the return value IS that result. Take the following example - a function which gives you the square root of a number.

float squareRoot = sqrt( 4.0f );

In this example, "4.0" is my input, "determining the square root of the input" is the computation, and "2.0" is the return value which will be stored in the variable squareRoot.

So whenever you're writing your functions and need to return a value, just remember that its the result of your computations, which are needed by the calling function (the function that called YOUR function ). Or, some flag indicating success or failure of your function.

Share this post


Link to post
Share on other sites
i was guessing that if it was a function that was returning a value after a calculation that he understood that, I kind of figured you was asking about why he was returning values from something that does not do a calculation.

theTroll

Share this post


Link to post
Share on other sites
Assuming C/C++, you should return 0 from main() to indicate to the environment (Operating System) that no errors occurred. Any other value may be interpreted as an error.

The function exit(value) does the same thing as returning value from main().

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