Return type

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 C example:

int calculateSum(int a, int b) {
    return a + b;
}

the return type is int. The program can therefore rely on the method returning a value of type int. Various mechanisms are used for the case where a subroutine does not return any value, e.g., a return type of void is used in some programming languages:

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:

return;

If a value from a method declared void is returned, a compiler error will occur.

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; for instance, one cannot return an int value from a method declared to return a bool.

The getArea() method in the Rectangle class that was discussed in the sections on objects returns an int:

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 width * height evaluates to.

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 may have a method like this:

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

  1. ^ Kernighan, Brian W.; Ritchie, Dennis M. (1988). The C Programming Language (2nd ed.). Prentice Hall. ISBN 0-13-110362-8.

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.