• Post Reply Bookmark Topic Watch Topic
  • New Topic

Code Snippet Help  RSS feed

 
Ben Jass
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I have posted here before, and I'm still having trouble understanding what return statements do. So I had an idea. I took this code snippet from Head First Java 2nd Edition and I was wondering if anyone could explain what the "return = 1;" does, and what the "return 0;" does. I understand that it returns the value of 1. What is the process of what happens when it goes through the return? I compiled it and all, I'm guessing that it adds 1 every time that the if statement's condition is true. As for the return 0;..I have no idea.
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ben,

In this case, if the parameter "index" you give to the method maybeNew() is less than 5, the method will return 1, otherwise it will return 0. What that means to your program is that where it makes a call to maybeNew(), it will get a value of 0 or 1 back, which it can then use in further calculations. Does that make sense to you?

Imagine you are running a business and you need to know how much profit you're making at each branch. You hand a book of figures to one of your minions, representing sales in the branch for the month, and tell him to crunch the numbers to figure out the profit for that branch. She goes off and runs the number crunching process that you've taught her and comes back with the figure $85. That's a return value. You can then get more return values from other minions and add them all together, or do whatever you want with them.
 
Ben Jass
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somewhat...I guess I narrowed down the problem I have. Where does it return the value too? I understand that when you think of returning something. It probably clicks in your brain as an easy thing to comprehend. For me it just doesn't. I don't see the purpose behind it. When do we know we have to use it. You know?
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See my story of minions that I edited into my original post. The purpose of methods is to put a defined set of work into a single place where it can be called upon from many places in your program. In that way it helps you break down a large problem into manageable tasks, and saves you from reinventing the wheel every time you write a program. The purpose of a return value is to show you the result of the method.

Take a square root for example. Do you know how to compute a square root? Even if you do, do you want to write that code every time you need the square root of a number? No, you want to put that into method and let it return an answer to you. In fact, Java has already provided a method to you: Math.sqrt(). The return value is the square root the number you give it as a parameter.

OK, so in your code the return value goes to:
count = count + m4a[x].maybeNew(x);

and makes it the same as:
count = count + 1;
or
count = count + 0;

at various times through the while loop.
 
Ben Jass
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm..It's coming a little clearer now...Why do I need to return it though? If we take the example that you gave me..

So...no what do you do with that return value? Does it have the same functionality as an variable that was declared previously?

---

Month 2--

*She does the process again, for the purpose of making this simple, it is a simple addition method*

int numberOne = 28$

int numberTwo = 56$

int totalProfitForTheMonth = numberOne + numberTwo;

---

Now that you have totalProfitForTheMonth declared. Does that value have the same functionality as returning a value?

Am I steering off in the wrong direction lol?
I'm probably making the concept a whole lot more complicated then it really is.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does that value have the same functionality as returning a value?

You could do everything in the main() method of your class. One big method. 10000 lines. You could. But you'll find out that it's too difficult to read, too difficult to maintain, too difficult to change... So you'll use methods. Methods return values. The meaning of the returned value depends on what the method is doing. A method could check if the sun is shining today, returning true or false. A method could check how much money's left in your pockets, returning some kind of numbers. A method could do something very clever and return a status code to tell the caller the result of its action (0:ok, -1:error).

Using methods helps you to reuse a piece of code too. Your example, int totalProfitForTheMonth = numberOne + numberTwo, is too simple. Imagine that you had this : int totalProfitForTheMonth = 2 * numberOne + numberTwo; What if you want to use this formula (2*x+y) in many places in your program. Are going to write the formula all around the place, or are you going to write a method returning the result ? What if your formula was wrong ? It was not 2*x+y but 2*(x+1)+y. It would save your day to have one method, wouldn't it ?

The Head First Java example is not a good example if you are trying to understand the meaning of a returned value. I'm looking at that method, and I have no clue what its purpose is. Try to go on and look for more explicit examples.
 
Ben Jass
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher, you clarified it up a lot. I appreciate that.

So let's say...



Would the method testReturn now be equal to 85? So I could call that anywhere? So if we use that example again:



Is that how it works? Oh my lord. I'm actually beginning to understand it. The whole point of returning is so you can get the value (boolean, int, etc) to come back to that method?

So if I called testingReturn() somewhere, it returns the value of 8. Now what? How can I use the int total now in the rest of the program, Can I suddenly call the int total anywhere in the method?



Would that work?

Oh boy this is exciting, thanks a lot.
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, you've just about got it! The problem with your last example:



is that "total" is a local variable in the the testingReturn() method. It's only the value that gets returned, not the name of the variable. So what you want is something like:



Otherwise, you've got the idea now.
 
Ben Jass
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, I can't thank you guys enough!
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It feels good when things become clear
 
Dimitrios Chatzidimitriou
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this code..



Could be like this.. ?



Or I took it totally in a wrong way?


And something else..

In the POC I posted.. If we delete the lines 5 and 6 would that make any difference in our results?

Thanks!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!