+ 1

Use of "return" in c++ ??

Please tell the use of return in c++ i program in c ++ and use this often and program runs but dont know exactly what does this stand for ? return 0; Please tell

21st Jun 2017, 1:58 PM
Subhankar Golder
Subhankar Golder - avatar
6 odpowiedzi
+ 8
@Elle Functions with return values have to be printed in order for the return value to be displayed. Void functions, on the other hand, has no return value and the cout statement is usually embedded inside the function. E.g. Compare: int function() { return 100; } and void function() { std::cout << 100; }
21st Jun 2017, 2:36 PM
Hatsy Rei
Hatsy Rei - avatar
+ 5
E.g. int function () { return 100; } int main() { std::cout << function(); return 0; } // outputs 100 The return statement sends a specific value back to the caller. For 'return 0' in main(), the value 0 is returned to your operating system which calls main() and run your program.
21st Jun 2017, 2:18 PM
Hatsy Rei
Hatsy Rei - avatar
+ 1
You see this when you call a function. The purpose of 'return' is to return a value from the function. It's the difference between me sending someone to the store so they can do what they want, and me sending someone to the store and telling them to bring me back my change. For example, lets say you create a function to add variable X to variable Y. Inside of your main loop, you call that function and have it add X = 5 and Y = 5 together. You're able to get the result because the function RETURNS the value back to you when it's done. x + y = z return z; It's simply returning values/results from a function back to the code that called the function. Hope this helps. Let me know if you need further elaboration upon this.
21st Jun 2017, 2:10 PM
AgentSmith
+ 1
@hatsy is it necessary to write cout in main() to have 100 or you can even call the function() alone?
21st Jun 2017, 2:24 PM
Elle
Elle - avatar
+ 1
The function call is "replaced" by the returned result. You can use the value however you wish: -print it: cout<<myfunc(); -store it: myvariable=myfunc(); -ignore it: myfunc(); and so on :) However, since the OP specifically mentioned the "return 0;", I believe s/he was referring to the return statement in the main function.
21st Jun 2017, 2:28 PM
Bogdan Sass
Bogdan Sass - avatar
0
It's the return value of your program. Other programs can capture that and make a decision based on it. It is customary to return 0 on success, and a non-zero value on failure.
21st Jun 2017, 2:10 PM
Bogdan Sass
Bogdan Sass - avatar