+ 2

What will occure if we don't use return keyword in the method?

for example if we create a method and instead printing its returned value from main method.we just print the result in the method without return... what is the difference?

5th Oct 2017, 5:30 AM
Super Programmer
Super Programmer - avatar
14 Respostas
+ 1
It depends on what you need to do. This will produce an error, because you're not returning an int which is the return type. If you need to get the value back from the function then you'd want to return it. You do this so that you can do something with it elsewhere in your program. If all you needed to do was add them together and print it out then this would suffice, but lets say you needed to get the value after they were added and perform another calculation on that value or if it needed to be set to some other instance variable etc, then you'd need to return it from the function.
5th Oct 2017, 6:31 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
#ChaoticDawg look at the code: int addition (){ int a=1,b=2; cout <<a+b; //what is difference of it with //return a+b; //and which is better and why?? }
5th Oct 2017, 6:10 AM
Super Programmer
Super Programmer - avatar
+ 2
If the methods/functions return type is void/Void then return isn't needed and can only be used by itself and without a trailing value. If there is another specified return type for the method/function then a compile time error will occur. In some cases there is a exception to this when it comes to the main/Main function/method. Such as with some compilers for C++ the return statement isn't needed and can be omitted, with a main function that has a return type of int, as it is implicitly applied via the compiler with a value of 0. (This is the case with the complier that is used in the playground). You can test this in the playground by writing some simple code in the main function for a C++ program and then remove the return 0; statement. It will compile and run fine. However, if you attempt to do this with a function or method other than main in a C++ program in the playground, you will receive an error.
5th Oct 2017, 5:41 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
In c# you get a compile time error. You can use void a return type than your method does not return anything. Otherwise you need to give a value like 0, "", null or something that makes more sense.
5th Oct 2017, 5:42 AM
sneeze
sneeze - avatar
+ 1
#ChaoticDawg I absolutely get nothing from your this answer.I knew whatever you said.I question is little bit different. and there is no problem if we print the result in the user defined method instead returning it to the main..so why it could be?
5th Oct 2017, 5:44 AM
Super Programmer
Super Programmer - avatar
+ 1
@Asina I'm sorry, not sure what you mean by third answer. Which part don't you understand. Maybe I can clarify it better.
5th Oct 2017, 5:52 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Sorry I am a c# specialist, so I can't tell much about c++. On the outside there is no difference, both methods are good. Both methods print the result of the addition to screen. You will see this al lot of times where you cannot differ code based on the output. The inside is personal preference and also a matter of usage. If it is a logger-method I like to keep the code in the method. If it is a calculation I propably need the value in the rest of my code an I wil use a int-type and return statement. //option 1 code in the method //method void addition() { int a=1,b=2; cout <<a+b; } //code in the main int main { addition(); //not sure if this is correct because it is a int } //option 2 use a return statement int addition() { int a=1,b=2; return a+b; } //code in the main int main { cout << addition(); //not sure if this is correct because it is a int }
5th Oct 2017, 8:39 PM
sneeze
sneeze - avatar
+ 1
thanks @sneeze
5th Oct 2017, 8:45 PM
Super Programmer
Super Programmer - avatar
+ 1
muthukarthimkp2020@gmail.com
23rd Feb 2020, 4:47 AM
Muthukaruppan.M
Muthukaruppan.M - avatar
+ 1
void
6th Dec 2020, 5:28 AM
Dhika Nur Aisyah
0
sneeze, thanks for ur answer..so can I ask what will he occure to the machine when we don't return the value and print itself in the method? specially in cpp it is possible...
5th Oct 2017, 5:46 AM
Super Programmer
Super Programmer - avatar
0
Thanks for continues answering. It took me a while before I fully understood your question
5th Oct 2017, 8:48 PM
sneeze
sneeze - avatar
0
Use void
6th Apr 2021, 10:36 AM
Joseph S. Lablah, Jr
Joseph S. Lablah, Jr - avatar
0
void
14th May 2021, 7:05 PM
W.B.Gayan Chamara Peiris
W.B.Gayan Chamara Peiris - avatar