+ 1
[Learning Java] What does "to return a value" mean? Im not a native speaker. Upd: I know what return means generally.
The return keyword can be used in methods to return a value. For example, we could define a method named sum that returns the sum of its two parameters.
8 Respuestas
+ 10
Example:
// method with return value:
public int sum(int x, int y){
// ^ this is the type of the return value
return (x+y);
// ^ here is the sum returned
}
If you call above defined method, it will return an int, you can assign to a variable:
int sum = sum(1, 2);
// ^ this calls the method and
// assigns the return value to your
// variable
You can then print the sum:
System.out.print(sum); // prints the value
// of the int sum,
// which is 3
You can also call your method within the print statement without assigning it to a variable:
System.out.print(sum(1, 2)); // prints 3
+ 9
Sorry, I don't speak Estonian. The English definition you posted is good, try to use Google translate.
+ 9
No. Return is used to receive (get back) a value from a method.
+ 9
Yes, that's it.
+ 1
Thank you.
0
Some synonyms or/and explanations what happens while using 'return' function might be helpful for me. It seems that I know return means to give sth back etc
0
So, return in this case means to perform a math action, to display sth.
0
If I say
public into sum(int x, int y) {
int z = x+y; // this is the main part of the method, isn't it?
return(z); // by that the value becomes accessible outside of the method, doesn't it?
}