Skip to content
CampusEduX

Control Flow · Lesson 35 of 150

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 min read

01What 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.

02Why 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.

03Real-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.

04Syntax

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

05Example 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

06Key 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.