Public Group

# Another newbie question,

This topic is 4524 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 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 on other sites
Ahhh, I see now, thanks a lot. These exercise programs had me returning 0 so I was confused a bit at its purpose.

##### 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 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().

1. 1
2. 2
Rutin
22
3. 3
4. 4
frob
17
5. 5

• 9
• 33
• 13
• 12
• 10
• ### Forum Statistics

• Total Topics
632577
• Total Posts
3007157

×