Return Type
   HOME

TheInfoList



OR:

In
computer programming Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as ana ...
, the return type (or result type) defines and constrains the
data type In computer science and computer programming, a data type (or simply type) is a set of possible values and a set of allowed operations on it. A data type tells the compiler or interpreter how the programmer intends to use the data. Most progra ...
of the value returned from a
subroutine In computer programming, a function or subroutine is a sequence of program instructions that performs a specific task, packaged as a unit. This unit can then be used in programs wherever that particular task should be performed. Functions may ...
or
method Method ( grc, μέθοδος, methodos) literally means a pursuit of knowledge, investigation, mode of prosecuting such inquiry, or system. In recent centuries it more often means a prescribed process for completing a task. It may refer to: *Scien ...
. In many
programming languages A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
(especially statically-typed programming languages such as C,
C++ C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
,
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's List ...
) the return type must be explicitly specified when declaring a function. In the Java example: public void setShuma(int n1, int n2) 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: public void returnNothing()


Returning a value from a method

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, 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: 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. The getArea() method in the Rectangle Rectangle class that was discussed in the sections on objects returns an integer: // A method for computing the area of the rectangle public int getArea() This method returns the integer that the expression evaluates to. The getArea method returns a primitive type. A method can also return a
reference type In computer programming, data types can be divided into two categories: value types (or by-value types) and reference types (or by-reference types). Value types are completely represented by their meaning, while reference types are references to ano ...
. For example, in a program to manipulate Bicycle objects, we might have a method like this: public Bicycle seeWhosFastest(Bicycle myBike, Bicycle yourBike, Environment env)


References

{{DEFAULTSORT:Return Type Subroutines Articles with example Java code