+ 1

why we use returne value in methods ? plz help

6th Nov 2016, 2:29 PM
ayoub
ayoub - avatar
2 Answers
+ 8
In fact, When compiler read return statment on a part of method, stop method. When we want the return have not value, using void return-type for declaring method, else We need to define a return-type for method (example int, double, etc.) —------------------------------------ void: it's optional to using return; in method body // Example 1 void write(String text) { System.out.println("-->" + text); } // Example 2 void write(String text) { if (text.equals("bye")) { return; // Just for stop method } System.out.println("-->" + text); } NOTE: We can use return without value, just for stop method. —------------------------------------ no void: It's Mandatory to using return with value in method body, and method must have return value. // Exmple 3 String getAnswer(String text) { if (text.equals("bye")) { return "See You"; } return "My english is not very well."; } NOTE: Finally,we must return a value from method when method return-tyoe isn't void.
6th Nov 2016, 3:43 PM
Hadi Akbarzadeh
Hadi Akbarzadeh - avatar
+ 1
thank you my bro for help 👍
6th Nov 2016, 8:05 PM
ayoub
ayoub - avatar