• Post Reply Bookmark Topic Watch Topic
  • New Topic

Advantages of having a return type  RSS feed

 
Citra Dil
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain me the scenarios where Return type(a method that returns a value) is useful..If possible please brief me with examples.
Below I am mentioning an example with void(doesn't return a value) and a int return type(returns a int value) .


 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A method called "calcArea" should do just that - calculate the area. It should not print it. Printing it should be handled by a method called "printArea" - which may have to call calcArea.

It is often the case you need to use a value more than once. I may have to use it 100 or 1000 times. If I have a method that doesn't return the value, sure I could call the method 1000 times...but if it takes 20 seconds to do that calculation, I'm going to be waiting a while.

Or, if the value can change from moment to moment, and I want a snapshot of it to use for a while, I'd need to get the value and save it. The only way to do this is to return the value and store it in a variable.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second exanple is pretty useless if you want to do anything other than print the value to System.out. What if you want to put it in a database, display it on a web page, or just use it in another calculation? You'd have to have a different method for every purpose, repeating the calculation each time. Or you can have a single method that returns the value and then just call that.
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Has anybody explained about static methods? Both your methods which calculate the area take the input as parameters, and don't use instance fields. So they might better be static themselves.
 
Edwin Torres
Ranch Hand
Posts: 57
Java Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Functions are good when you want the value returned immediately. For example:

Another example is recursive functions:

As Campbell said, your method should be static. It's not necessary to create an object just to call that method. Make it static and call it like this:
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would prefer to write the factorial method like thisThat is what the ?: operator was designed for, and avoids a double return. It is worth adding Exceptions if the argument is too big (I forget, but I think 22! is the largest factorial which will fit into a long) or negative.

It is worth remembering what a function is: it returns one result, and really ought always to return the same result for the same input. Factorial, sine, logarithm, square, etc are functions. So is a phone book (or rather was, in the days before people carried several mobiles, and only had one phone number each ), and you can easily represent a phone book as a Map. That is why that link says
A Map . . . models the mathematical function abstraction. Their italics.
 
Edwin Torres
Ranch Hand
Posts: 57
Java Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:...It is worth adding Exceptions if the argument is too big (I forget, but I think 22! is the largest factorial which will fit into a long) or negative.

Man you're good! Nice.
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edwin Torres wrote: . . . Man you're good! Nice.
Aww shucks! You've got me all embarrassed

It's simply standard defensive programming, considering what will breach the class invariants (or in this case produce an arithmetic overflow or a stack overflow) and taking notice.
 
Edwin Torres
Ranch Hand
Posts: 57
Java Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It's simply standard defensive programming, considering what will breach the class invariants (or in this case produce an arithmetic overflow or a stack overflow) and taking notice.

I'm new to this forum. I probably should strive to write my best code all the time and not be lazy.
 
Citra Dil
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your wonderful responses. I am a total beginner to Java and started learning myself last week. I wrote the above program (yeah right…Object is created just to have the feel of OO )to see the passing value in methods. But for me it is something like,
Without using Return:
total = a + b
Call this using
calcTotal(int x,int y) [x=2;y=3] and you get result as 5.

Using Return:
total = a +b
Return total
And when you call the method you get the value returned in total.

For me it served the purpose both the ways. I couldn’t convince myself on the difference in usages of void(returns a null value) and return(returns a proper value). I am sure there would be different angles to approach this.I am partially clear now. And there is much more to learn.I still have doubts. Sorry I would like to get more clarity on basics

you definitely get the value for balance.

In both the cases it passes the value 2.0 to the parameter in method’s (add/getInterest) header.So inside the add/getInterest method, the value of rate becomes 2.0 and so the further calculations and the result.
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be careful to spell all code correctly in code tags: it's "void" not "Void", and make sure to put all {} in. I had to add } for you, otherwise it was very confusing to read.
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is, Void is a valid return type. However, it requires a return value. Since Void cannot be instantiated you can (and must) therefore return null.
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote: . . . Void cannot be instantiated . . .
It seems so appropriate that Void cannot be instantiated
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!