This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Cattle Drive and the fly likes basics: return value to main Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "basics: return value to main" Watch "basics: return value to main" New topic
Author

basics: return value to main

Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
I would like to use return to pass a value from one method call to the next in main. Tried to find something about this in a couple books with as yet no success...
What happens to a value that gets returned from a method? How can I lassoe it for the next method call?
Thanks for your help once again,
Pauline
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5089

Dont know which one our nitpicker prefers .
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Hi jdj, thanks for the tips. I had actually tried those 2 options, but the methods hadn't been declared as private. That's my next step. What's the reason behing the "privacy" of the method?
(Guess I could look it up in a book somewhere, but that'd be less fun...)

Pauline
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Still trying, no success yet.
Tried with private method but I'm still getting a compiler error that the method isn't found. My case is slightly different, I'm trying to pass the return value of a method to the second call of the same method, but with different parameters.

Is this a special case or am I greener than green today?
cheers,
Pauline

[This message has been edited by Pauline McNamara (edited May 18, 2001).]
Rick Prevett
Ranch Hand

Joined: Apr 16, 2001
Posts: 68
Pauline, the thing I see is that in your method calls you don't have int j = methodDoSomething(xxx,xxx,xxx). So your returnValue has nowhere to go.
rick

Originally posted by Pauline McNamara:
[B]Still trying, no success yet.
Tried with private method but I'm still getting a compiler error that the method isn't found. My case is slightly different, I'm trying to pass the return value of a method to the second call of the same method, but with different parameters.

Is this a special case or am I greener than green today?
cheers,
Pauline

[This message has been edited by Pauline McNamara (edited May 18, 2001).][/B]

Paul Ralph
Ranch Hand

Joined: Aug 10, 2000
Posts: 312
Specifically for your code , replace:
methodDoSomething( thisParam , thatParam , anotherParam ) ;
with:
int returnValueOfFirstDoSomethingCall = methodDoSomething( thisParam , thatParam , anotherParam ) ;
Paul R
[This message has been edited by Paul Ralph (edited May 18, 2001).]
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Thanks folks. I got the return value where I wanted it, finally. Progress in baby steps.
Here's another sticky detail: my method DoSomething also prints out stuff. When I put in a line like you suggested to get the return value:
...
methodDoSomething( thisParam , thatParam , anotherParam ) ;
int i = methodDoSomething( thisParam , thatParam , anotherParam ) ;
methodDoSomething( i, blah, bloo ) ;
...
the method seems to get called on the first AND second line too so the stuff gets printed out twice (correctly, but twice).
Maybe I'm trying to go down the wrong road?
Really appreciate your input.
cheers,
Pauline
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
I finally got it. Thanks you guys. And thank you Marilyn: one harmless looking little nitpick and I got a lesson in combining value assignment with the method call, mixed in with a brush-up on precedence.
In the end (or rather, for the latest submission of this assignment), the method call looks like you suggested, Paul:
........
variablePassedAsParam = methodDoSomething( variablePassedAsParam, anotherParam, yetAnotherParam ) ;
methodDoSomething( variablePassedAsParam, anotherParam, yetAnotherParam ) ;
..........
If I've understood correctly, the method call gets evaluated first, prints out its stuff and returns a value for assignment to the variable, which then gets used in the next call of the method.
Ho Doggie.
To my inexperienced eye it makes main look a lot less simple... anyway, I can submit it and go out and play now.
Richard Boren
Ranch Hand

Joined: Mar 01, 2001
Posts: 233
Why do I see/feel/think recursion when I look at this?


Originally posted by Pauline McNamara:
variablePassedAsParam = methodDoSomething( variablePassedAsParam, anotherParam, yetAnotherParam ) ;
methodDoSomething( variablePassedAsParam, anotherParam, yetAnotherParam ) ;

I'm sorry I can't help it
[This message has been edited by Richard Boren (edited May 19, 2001).]
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Hi Richard, yes it should even smell like recursion because it's what used to be a loop. Since it's loop days, though, it went through the same considerations/nitpicks as those brought up by Paul R in the Say 4b technique thread.
I couldn't help thinking loop either, but now I'm getting help to think differently...

Pauline
Richard Boren
Ranch Hand

Joined: Mar 01, 2001
Posts: 233
Boy I almost wrote a really stupid reply to your last post. I'm glade I re-read it before I clicked the submit button. I thought you were calling it a loop
Thanks for clearifying that for me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: basics: return value to main