Return type
Type of data able to be returned by a function or method
title: "Return type" type: doc version: 1 created: 2026-02-28 author: "Wikipedia contributors" status: active scope: public tags: ["subroutines", "articles-with-example-java-code"] description: "Type of data able to be returned by a function or method" topic_path: "general/subroutines" source: "https://en.wikipedia.org/wiki/Return_type" license: "CC BY-SA 4.0" wikipedia_page_id: 0 wikipedia_revision_id: 0
::summary Type of data able to be returned by a function or method ::
In computer programming, the return type (or result type) defines and constrains the data type of the value returned from a subroutine or method. 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 C example:
::code[lang=c] int calculateSum(int a, int b) { return a + b; } ::
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:
::code[lang=c] void sayHello() { printf("Hello, world!"); } ::
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.
To declare a method's return type, it is included in its method declaration. Within the body of the method, the return statement is used to return the value.
Any method declared void does not 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:
::code[lang=java] return; ::
If a value from a method declared is returned, a compiler error will occur.
Any method that is not declared must contain a return statement with a corresponding return value, like this:
::code[lang=java] return returnValue; ::
The data type of the return value must match the method's declared return type; for instance, one cannot return an value from a method declared to return a .
The method in the class that was discussed in the sections on objects returns an : ::code[lang=java] class Rectangle { private int width; private int height; // ...
// A method for computing the area of the rectangle
public int getArea() {
return width * height;
}
} ::
This method returns the integer that the expression evaluates to.
The method returns a primitive type. A method can also return a reference type. For example, in a program to manipulate objects, we may have a method like this: ::code[lang=java] class Bicycle { // bicycle class }
class RaceEnvironment { // environment class }
public class BicycleRace { // ...
public Bicycle playBicycleRace(Bicycle bike1, Bicycle bike2, RaceEnvironment env) {
Bicycle winner;
// Code to calculate which bike is
// faster, given each bike's gear
// and cadence and given the
// environment (terrain and wind)
return winner;
}
} ::
References
References
- (1988). "[[The C Programming Language]]". Prentice Hall.
::callout[type=info title="Wikipedia Source"] This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page. ::