Lesson 19 of 34

Return Values

9 min 30 XP

To give a value back, replace void with its type (int, double, boolean, String) and use return. The method ends as soon as return runs.

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

A good method does one job and its name says what: isPrime, findMax, calculateTax.

Challenge

+30 XP

Write a method static long factorial(int n). In main(), read n and print factorial(n). Input: 5 → Output: 120

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.