+ 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.

16th Jun 2017, 9:52 PM
Mihai Tanasa
Mihai Tanasa - avatar
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.
16th Jun 2017, 10:48 PM
Mario L.
Mario L. - avatar
+ 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
17th Jun 2017, 12:02 AM
cHiRaG GhOsH
cHiRaG GhOsH - avatar
+ 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.
17th Jun 2017, 12:44 AM
Jamie
Jamie - avatar