Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Returning Things in Methods
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
sliverofshadows475




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: 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?
sliverofshadows475




PostPosted: 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?
Insectoid




PostPosted: 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.
sliverofshadows475




PostPosted: 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.
andrew.




PostPosted: 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.
TheGuardian001




PostPosted: 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().
DemonWasp




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: