+ 3
What is "return" really doing.(Java)
I'm new to programming and java is my first language but I don't understand what "return" dose what is returning to and where is returning.
3 Answers
+ 4
let's say you have a function which adds two variables. but you don't want that function to print the result to the screen. so you return that result.
function int addition(int a, int b) {
return a+b;
}
(don't kill me if that's wrong, I'm not good at java)
so you give that function two variables, it calculates the sum and returns it.
you could pass that to another variable like
int sum = addition(5, 7);
or you could print it to the screen:
System.out.println(addition(5, 7));
that's what it does. it returns the value to the function calling it.
this comes in handy if you have to calculate values for different variables but all in the same (maybe complex) way. you don't want to rewrite the calculation over and over again. use functions instead and just return the result.
+ 6
@mario the declaration of a method is as follows:-
int addition(int a,int b) {
return a+b;
}
p.s. no function keyword is used..:D
+ 4
Well what return is really doing is storing a pointer to the return value and then jumping back to the address the "function"(method) was called from. Lol, you asked.