Return Statement Example Java Program


Definition

The return statement returns value to the caller. Any time the return statement is called, the process is executed and the value is transferred to the caller. 

Syntax

return;
(or)
return value;

Return Statement Example Program

import java.util.Scanner;

class ReturnStatementExample{
	public static void main(String[] args){
		System.out.println("Jump Statement Example");
		System.out.println("Enter 2 numbers for adding");
		Scanner scanner = new Scanner(System.in);
		int num1 = scanner.nextInt();
		int num2 = scanner.nextInt();
		int additionResult = additionMethod(num1,num2);
		System.out.println("Addition Result is : "+additionResult);
		System.out.println("Enter 2 numbers for subtracting");
		int num3 = scanner.nextInt();
		int num4 = scanner.nextInt();
		int subtractionResult = subtractionMethod(num3,num4);
		System.out.println("Subtraction Result is : "+subtractionResult);
	}
	
	static int additionMethod(int num1, int num2){
		int result = num1+num2;
		return result; //returns the result to the caller
	}
	
	static int subtractionMethod(int num1, int num2){
		int result = num1-num2;
		return result; //returns the result to the caller
	}
}

Sample Output

Jump Statement Example
Enter 2 numbers for adding
40
50
Addition Result is : 90
Enter 2 numbers for subtracting
60
40
Subtraction Result is : 20