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.
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.
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.
Without using Return:
total = a + b
Call this using
calcTotal(int x,int y) [x=2;y=3] and you get result as 5.
total = a +b
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.