In computer programming, the return type (or result type) defines and constrains the data type of the value returned from a subroutine or method.[1] In many programming languages (especially statically-typed programming languages such as C, C++, Java) the return type must be explicitly specified when declaring a function.
In the Java example:
public int getShuma
the return type is . The program can therefore rely on the method returning a value of type . Various mechanisms are used for the case where a subroutine does not return any value, e.g., a return type of is used in some programming languages:
A method returns to the code that invoked it when it completes all the statements in the method, reaches a return statement, orthrows an exception, whichever occurs first.
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 return statement can be used to branch out of a control flow block and exit the method and is simply used like this:
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:
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.
The getArea method in the Rectangle Rectangle class that was discussed in the sections on objects returns an integer:
The getArea method returns a primitive type. A method can also return a reference type. For example, in a program to manipulate Bicycle objects, we might have a method like this: