+ 4

What is the point of return

Basically, from what I have read, return is supposed to store the value in memory. How are you supposed to access this value then? I wrote this code attempting to access the value but if the only way to access it is by declaring a variable, I don't see the point using return in the first place.. As for returns without a value you can use break.. So why bother with return statements at all? Below is the code I wrote. calling sum with 2 values alone doesn't output a value but im not interested in outputting it directly im interested in returning it and accessing it at a later point. class MyClass { static int sum(int val1, int val2) { return val1 + val2; } public int getsum(){ return sum; } public static void main(String[ ] args) { sum(2, 5); getsum(); } }

21st Apr 2020, 10:04 PM
Christian Jacob O'Neill
Christian Jacob O'Neill - avatar
25 Respostas
+ 7
Christian Jacob O'Neill return means that it returns the value. By calling a method you jump in, do the stuff inside the method and with return you jump out of the method, back to the point where you have called the method. int i = sum(2,5) here you call sum -> do the math val1 + val2 -> return the value back to i. So that i = 7.
21st Apr 2020, 10:42 PM
Denise RoƟberg
Denise RoƟberg - avatar
+ 7
I think it may be harder to understand as long as you're thinking only from one class. One of the points of having classes and their instances in the first place is that each object can protect its data. So class A wouldn't be able to get value x from class B. Now with return, you can create a 'door' through which such a value can be transported, for example by a getter method. instance a (from class A) would call b.get(), and that method would then hand over the value to a by using return. Why protect data in the first place if you hand it out anyway? Well, the difference is that b decides from the inside, if, under what circumstances and how this value is shared. a can only ask.
21st Apr 2020, 10:44 PM
HonFu
HonFu - avatar
+ 4
Christian Jacob O'Neill Another example. You change the return type to void and print the value. But then you can't do anything with the value. It is printed, not more, not less. If the method returns a value you can assign it to a variable and work with it. And of course it gives you more control. Inside a method you can define what should happen and what's not allowed.
21st Apr 2020, 11:05 PM
Denise RoƟberg
Denise RoƟberg - avatar
+ 3
I think I'm starting to understand more now thanks everyone!
21st Apr 2020, 11:36 PM
Christian Jacob O'Neill
Christian Jacob O'Neill - avatar
+ 2
coffeeunderrun HonFu thanks! Denise RoƟberg so you're saying it's mainly for flow control?
21st Apr 2020, 10:57 PM
Christian Jacob O'Neill
Christian Jacob O'Neill - avatar
+ 2
U can aswell assign the method to a variable when you call it
22nd Apr 2020, 2:22 AM
Idoko Emmanuel
Idoko Emmanuel - avatar
+ 2
Underneath your query, word java is observed. Hence it is construed that the question relates to java but not to meaning in lexicon. You may find answer in https://www.geeksforgeeks.org/return-keyword-java/.
22nd Apr 2020, 3:00 AM
narayanaprasad
narayanaprasad - avatar
+ 2
generally, it allows the script's user to retrieve the result.
22nd Apr 2020, 9:55 PM
Adnan Sabbab
Adnan Sabbab - avatar
+ 1
That defeats the point doesn't it? I'd just write static int sum(int val1, int val2) { result= (val1 + val2) Then I don't use return.. That's why I'm asking what is the point of return? It doesn't do anything because it doesn't output val1+val2 and if there's no way to access the value returned it is a pointless statement??
21st Apr 2020, 10:17 PM
Christian Jacob O'Neill
Christian Jacob O'Neill - avatar
+ 1
I'm not up-to-speed on java..but.. class MyClass { static int value; static void sum(int val1, int val2) { value = val1 + val2; } static public int getsum(){ return value; } public static void main(String[ ] args) { sum(2, 5); System.out.println(getsum()); } }
21st Apr 2020, 10:22 PM
rodwynnejones
rodwynnejones - avatar
+ 1
Coffeeunderrun That achieves the same thing as mine though, if I type System.out.println (result) with your code or mine it does the same thing.. I thought result was supposed to store the value to the method instead of having to declare a new variable. If I have completely missed the point of what return is actually for then please excuse my ignorance but I don't see what I can use that for that I couldn't do already?
21st Apr 2020, 10:22 PM
Christian Jacob O'Neill
Christian Jacob O'Neill - avatar
+ 1
coffeeunderrun sorry I should have been more clear I'm referring to the code in my first response to you where I changed the method of sum to result = (var1+var2) instead of using the return statement
21st Apr 2020, 10:28 PM
Christian Jacob O'Neill
Christian Jacob O'Neill - avatar
+ 1
Yeah I just tried that and you were right. I'm not specifically trying to achieve anything from this code as it is pointless when there's the math package anyway. I'm using this example as it is the one given in the lesson https://www.sololearn.com/learn/Java/2153/ And I was trying to understand what is the advantage of using return statement in that use case over using something like this code that I wrote https://code.sololearn.com/ctBWtiti5sq6/?ref=app Does that make it any clearer what I am struggling to understand? There just doesn't seem to be a point using return from what example the lesson gives..
21st Apr 2020, 10:36 PM
Christian Jacob O'Neill
Christian Jacob O'Neill - avatar
+ 1
Do you want the getsum() to actually print the result ?
21st Apr 2020, 10:42 PM
rodwynnejones
rodwynnejones - avatar
+ 1
return type is necessary to specify in any method to tell the type of value returned by it. Once returned to a function call statement the value is gone if you do not assign it to a variable
22nd Apr 2020, 5:32 PM
Chetan Singh
Chetan Singh - avatar
+ 1
You donā€™t have to return. i think it depends on your method for class. If purpose of your method is to spit out data then you may not want to return. That can be done by caller or factory
22nd Apr 2020, 7:31 PM
\ā€¢/
\ā€¢/ - avatar
+ 1
return is used when makes calculation and need a result. Eg. function myFunction(a, b) { return a * b; } myFunction(5, 6); // Return will give the result.
22nd Apr 2020, 8:22 PM
Vishal Kumar
Vishal Kumar - avatar
+ 1
Learn about the data structure called a stack. This will help you to understand what return does.
22nd Apr 2020, 8:24 PM
Logomonic Learning
Logomonic Learning - avatar
+ 1
Basically to disrupt the int main function Returning the code back to zero
22nd Apr 2020, 9:35 PM
Arinze Chiduso
Arinze Chiduso - avatar
+ 1
It is used to hold the control to exit the function by throwing some output from it. In general,Ā returnĀ will be used to get the value/variable that being used in the function's scope.
23rd Apr 2020, 4:21 AM
Bikender Kushwaha
Bikender Kushwaha - avatar