Author |
Message |
sliverofshadows475
![](http://compsci.ca/v3/uploads/user_avatars/454875928494e809837490.jpg)
|
Posted: Sun May 09, 2010 1:46 pm Post subject: Returning Things in Methods |
|
|
Hey guys,
I was wondering if it was possible to return things to the main like this:
Main --parameters--> method --parameters--> another method --return--> main
code: |
class ReturningThings {
public static void main (String[] args) {
int i = methodthing();
System.out.print (i);
}//end main
public static void methodthing() {
othermethod();
}
public static int othermethod() {
return(4);
}
}//end class
|
This approach doesn't work out. Is there a way in which othermethod can return a variable to the main (without using an array)?
Thanks,
+SS |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Zren
![](http://compsci.ca/v3/uploads/user_avatars/1110053965512db6185954b.png)
|
Posted: Sun May 09, 2010 2:04 pm Post subject: Re: Returning Things in Methods |
|
|
MethodThing() doesn't return anything so how could it set i to it? If you want to call A, but A calls B, then B has to return to A before A can return to you (main). Here's a classic example of functions.
code: |
print( foo() ) // OUTPUT: "foobar"
string foo() {
return "foo"+bar()
}
string bar() {
return "bar"
} |
Output = foo()
foo() = "foo"+bar()
bar() = "bar"
foo() = "foo"+"bar"
Ouput = "foobar"
That help? |
|
|
|
|
![](images/spacer.gif) |
sliverofshadows475
![](http://compsci.ca/v3/uploads/user_avatars/454875928494e809837490.jpg)
|
Posted: Sun May 09, 2010 3:24 pm Post subject: Re: Returning Things in Methods |
|
|
Yeah, I understand you could do that, but in my program, it would be very much a hassle go from main --> method1 --> method2 --> method1 --> main, when I could just do main --> method 2 --> main.
Is there a way to directly go from method2 --> main? |
|
|
|
|
![](images/spacer.gif) |
Insectoid
![](http://compsci.ca/v3/uploads/user_avatars/13760332514cbd0ce972eaa.jpg)
|
Posted: Sun May 09, 2010 3:28 pm Post subject: RE:Returning Things in Methods |
|
|
Just call method2. Nothing is stopping you.
code: |
public static void main (whatever){
int foo = method2();
}
public int method1(){
}
public int method2(){
return 5
}
|
Oh, and I now have 1337 posts. |
|
|
|
|
![](images/spacer.gif) |
sliverofshadows475
![](http://compsci.ca/v3/uploads/user_avatars/454875928494e809837490.jpg)
|
Posted: Sun May 09, 2010 3:34 pm Post subject: RE:Returning Things in Methods |
|
|
Well actually, yeah, stuff is stopping me. Basically it goes menu --> showing and answering questions, but I want to pass back the score and which questions were answered properly without passing them back to the menu. >.<
I know it's a weird problem, but thanks for you guys' help. |
|
|
|
|
![](images/spacer.gif) |
andrew.
|
Posted: Sun May 09, 2010 3:37 pm Post subject: RE:Returning Things in Methods |
|
|
I'm pretty sure you must do it like this:
main → method 1 → method 2 → method 1 → main
You could also do this:
main → method 2 → main
But then you aren't calling method 1. In any case, I don't believe there is a way to call main. |
|
|
|
|
![](images/spacer.gif) |
TheGuardian001
|
Posted: Sun May 09, 2010 4:48 pm Post subject: Re: Returning Things in Methods |
|
|
It's not possible to jump directly back to main (that's not how program flow works), but I don't see how it's any harder to simply call return method2() inside of method1(). |
|
|
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: Mon May 10, 2010 9:23 am Post subject: RE:Returning Things in Methods |
|
|
There are several mechanisms that would let you do this, including one that Java features. I do not recommend the use of any of these for your problem.
Exceptions - if you throw an exception from method2 that isn't caught by method1 but is caught by main, then control will seem to flow through method1 without executing anything inside. In reality the stack is still unwound the same way, but your code doesn't look like it at first glance. I don't recommend this because exceptions are supposed to be the exception, not the common case.
Labeled Jumps - some languages (not Java) support jumping between methods arbitrarily. I really really strongly do not recommend this because it leads to ridiculous flow-of-control problems where you have no idea who's calling what where, why and how.
What I do recommend is that you package up all the results into a Results object or similar, then you either return them up the call stack (what you're trying to avoid) or you have some object with a .setResults() method that method2 will call before returning. In the latter case, you're probably passing this results-tracking object down the call hierarchy. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
|