java return

what is return in java??please dont say that it returns the value ???explain with an example

22nd Mar 2017, 2:14 PM
Tech Galaxy
Tech Galaxy - avatar
4 Answers
+ 16
Suppose you're calling a method to perform a task from another method (caller). Does your caller method need to know the result? If yes, then you must return the result from called method. Otherwise you may simply write a void method to perform the task. public static void multiply (int a, int b) { System.out.println(a*b); // no return, caller method won't know the result } public static int multiply (int a, int b) { return a*b; // the result is passed to the caller method }
22nd Mar 2017, 3:07 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 7
Consider a function, add, it takes 2 parameter x and y. int add(x,y) return (x+y); This function will RETURN x+y Example if we use it, a=add(5,4); Here, x=5,y=4 and add will return 5+4 .Hence the value of a will be 9(BUT it will not be printed on screen unless you give print(a) command)
22nd Mar 2017, 2:41 PM
Meharban Singh
Meharban Singh - avatar
+ 3
@Meharban Note* you cannot return an int with a void return type. so: int add(..x, ..y){ return x+y; }
22nd Mar 2017, 3:06 PM
Rrestoring faith
Rrestoring faith - avatar
+ 1
"It does not need to contain a returnstatement, but it may do so. In such a case, areturn statement can be used to branch out of a control flow block and exit the method and is simply used like this: return; If you try to return a value from a method that is declared void , you will get a compiler error." https://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html
22nd Mar 2017, 3:11 PM
Eranga - avatar