What is the difference between the RETURN and VOID types of method? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the difference between the RETURN and VOID types of method?

where do we use each of them?

6th Dec 2016, 2:57 PM
Nochains
Nochains - avatar
2 Answers
+ 2
A method returns to the code that invoked it when it completes all the statements in the method,reaches a return statement, or throws an exception. You declare a method's return type in its method declaration. Within the body of the method, you use the return statement to return the value. Any method declared void doesn't return a value. It does not need to contain a return statement, but it may do so. In such a case, a returnstatement can be used to branch out of a control flow block and exit the method and is simply used like this: return; If you try to return a value from a method that is declared void, you will get a compiler error. Any method that is not declared void must contain a return statement with a corresponding return value, like this: return returnValue; The data type of the return value must match the method's declared return type; you can't return an integer value from a method declared to return a boolean.
6th Dec 2016, 3:03 PM
Vipul Walia
Vipul Walia - avatar
0
thank you
6th Dec 2016, 3:37 PM
Nochains
Nochains - avatar