Skip to content
C

return

The return statement immediately exits a method and, if the method has a return type, sends a value back to wherever that method was called from.


1. What is the return Statement?

The return statement immediately exits a method and, if the method has a return type, sends a value back to wherever that method was called from.

2. Why is it used?

Methods often need to send a result back to the calling code — like a method that calculates and returns a total price. return is how that result is handed back.

3. Real-Life Example

Think of ordering food at a counter — you place your request, and the counter staff hands back your finished order. The return statement is like handing back that finished result to whoever asked for it.

4. Syntax

java
returnType methodName() { return value; // sends value back and exits the method }

5. Example Program

java
public class ReturnDemo { static int square(int number) { return number * number; } public static void main(String[] args) { int result = square(5); System.out.println("Square: " + result); } }

Output:

Square: 25

6. Key Points to Remember

  • A method with a void return type can still use return; alone, just to exit early, without sending back any value.
  • Once return executes, no further code in that method runs.
  • The type of value returned must match the method's declared return type.