+ 1

Confused about return and void

Hey com, I am learning C++ atm and got a bit confused with return, because I have no picture of it, I mean I could just learn it, but I want to understand this. I.e. what is the difference between cout and return + what is return 0 ? I also have difficulties understanding void, I know that void main is kinda ignorant, but in which case using void is necessary or even essential ?

1st Apr 2018, 12:14 AM
7 Answers
+ 9
Return and cout is not similar in any way. Cout sends data to the output. And return replaces the method with the returned value. int a = b(); int b() { return 10; } The above function is same as the one below. int a = 10 People also sometimes use return false when they want to stop the method from executing Return 0 is same as return false or return without any value. bool test(bool bl) { if(bl == true) { cout << "true"; return; } cout << "false"; } Every method must have a return type like bool or int. But if you don't want to return anything you use void.
1st Apr 2018, 12:19 AM
Toni Isotalo
Toni Isotalo - avatar
+ 5
Jakub Stasiak Yes I sometimes mess up the languages
1st Apr 2018, 12:30 AM
Toni Isotalo
Toni Isotalo - avatar
+ 3
The use behind the return statement is to return a value to the thing that is calling the function. For example... int add(int a, int b) { return a+b; } // inside main function int c = add(7, 3); // value of c is 10 now This little program makes a function which adds 2 numbers, after a variable called c is made and calls the add function which adds 7 and 3 and gives c the result. Now for the compiler to compile the C++ code it must know the return type (placed before function name) which is the type of data the return statement will return in this case an int because args a & b are ints so it makes sense. Now sometimes you don’t want to return anything which is when you use void to tell the compiler that the function will never return anything this can be used to for example edit some values outside the function. The difference between cout and return is that, cout displays text to the console meanwhile return returns a value from a function. Return 0 is used in the main function because it’s return type is normally int and if you don’t include it the compiler does for you. Return 0 tells anything else that will use the program (main function) that the program ran successfully without any errors.
1st Apr 2018, 12:26 AM
TurtleShell - avatar
+ 2
The main() should return in integer value. The return value of main() is consider the status of the function. Returning 0 means the function was successful with no errors! Return in other functions is what returns from the function being processed. What is returned should be the declared type. Ex: bool myFunction(){ if(number <= 20){ return true; }else{ return false; } } Void returns nothing!
1st Apr 2018, 12:29 AM
William Then
William Then - avatar
+ 2
@TurtleShell Thank you !
1st Apr 2018, 12:31 AM
+ 1
Toni Isotalo Your function would end with massive error. I dunno if that's some kind of pseudocode but by "cin" I guess you meant C++, so: 1. I highly doubt that it's legal to compare "bool" keyword to true/false. 2. Loading console input to r-value isn't the best idea aswell. Edit: point 2 is fixed :D Edit2: point 1 fixed aswell, it's all good now ^^
1st Apr 2018, 12:28 AM
Jakub Stasiak
Jakub Stasiak - avatar
+ 1
What is returned by functions that don't have a return statement? None False 0
12th Sep 2020, 11:20 AM