Lesson 19 of 34

Return ValuesReturn Values

9 min 30 XP

Method ஒரு மதிப்பைத் திருப்பித் தர வேண்டுமெனில் void-க்கு பதிலாக அந்த type-ஐ (int, double, boolean, String) எழுதி return பயன்படுத்துங்கள். return இயங்கியவுடன் method முடிந்துவிடும்.

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.

நல்ல method ஒரே ஒரு வேலையைச் செய்யும், அதன் பெயர் அந்த வேலையைச் சொல்லும்: isPrime, findMax, calculateTax.

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

Challenge

+30 XP

static long factorial(int n) என்ற method எழுதுங்கள். main()-ல் n-ஐப் படித்து factorial(n)-ஐ print செய்யுங்கள். Input: 5 → Output: 120

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.