This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

basics: return value to main

 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5089
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dont know which one our nitpicker prefers .
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 313
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic