+ 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?
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.
+ 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??
}
+ 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.
+ 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.
+ 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?
+ 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.
+ 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
}
+ 1
thanks @sneeze
+ 1
muthukarthimkp2020@gmail.com
+ 1
void
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...
0
Thanks for continues answering. It took me a while before I fully understood your question
0
Use void
0
void