Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Value returned from function  RSS feed

 
Amit Sethi
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q This function is returning int value :

class Try {
int function() {
return 20;
}
public static void main(String[] args) {
Try t1 = new Try();
t1.function(); // function called here but no one is going to recieve the returned value. Why java don't care about the return value & if we not recieve returned value then where it goes? Please explain.
}
}
// no compile time error:

Thanks & regards
Amit Sethi.
 
Philip Plenckers
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

<Why java don't care about the return value>
I guess you have to ask yourself why should Java care? Maybe you just want the code to be executed but you don't really care about the result. And if you don't care, why should java?

<& if we not recieve returned value then where it goes? >
It is just not there ...
But as long as you have a reference to the instance you just created it safely remains in there, and you can go back there and ask for it. But as soon as the instance pops off the stack it is lost.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amit Sethi:
Q This function is returning int value :

class Try {
int function() {
return 20;
}
public static void main(String[] args) {
Try t1 = new Try();
t1.function(); // function called here but no one is going to recieve the returned value. Why java don't care about the return value & if we not recieve returned value then where it goes? Please explain.
}
}
// no compile time error:

Thanks & regards
Amit Sethi.

In a pure functional language, your method is called a "constant function". There is absolutely no point calling a constant function unless you assign its return value. In Java (and not pure FP), there is always the possibility of a method acting in such a way as to have an effect on "world state" which means there may (or may not) be legitimate reason to ignore the return value and invoke the method to have a world state observable effect.

To elaborate further requires knowledge that is certainly not appropriate for a beginner's forum (or any Java forum?) but it is sufficient to say that since Java is incomplete - it cannot provide proof that a method does not effect "world state" - there is no reason to fail compilation should a "constant function" (according to you - not Java) return value be ignored.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Amit Sethi:

Why java don't care about the return value & if we not recieve returned value then where it goes? Please explain.


You have to declare a place to put it.

This is called a named variable.


This assumes checkStatus has been written somewhere, and that it returns a boolean value - which the compiler will tell you about if it doesn't.

Java is a machine, care doesn't enter into it.
 
Srinivas Kalvala
Ranch Hand
Posts: 257
Firefox Browser Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Any code which we write, if not serving the real time purpose, then it is of no use.

We are here to write something, which makes the human life much better.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Srinivas, why would code that is obviously intended to learn a point about the language need to perform a useful function? Doesn't learning about something make the world a better place (at least the world of the one who learns)?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was a little disappointed to discover that Java didn't allow the "C" syntax of casting the result of a function to void.


If the Java designers had felt really pedantic, this could even have been mandated (you can't ignore the result of a method unless you cast to void). But anyway, Java doesn't allow this, so I just stay mildly disappointed!
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not clear to me what you're missing, not being able to treat void as a typeless catch-all.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why should the language force you to catch the returned value??? even if it did, it couldn't force you to then USE that value. i mean, why is it better to force you to do this:



temp could immediatly falls out of scope, and the value is still lost. even if it doesn't drop out, there's nothing that is going to force me to use the value anywhere else in the code. nothing is gained by forcing you to catch it.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by fred rosenberger:
Why should the language force you to catch the returned value??? even if it did, it couldn't force you to then USE that value. i mean, why is it better to force you to do this:



temp could immediatly falls out of scope, and the value is still lost. even if it doesn't drop out, there's nothing that is going to force me to use the value anywhere else in the code. nothing is gained by forcing you to catch it.


If it could be proven that function() was indeed a mathematical function (and therefore, a constant function in this case), then the compiler *should* (must?) mandate that the return value is used since there is no other legitimate use case. Just like there is no legitimate use case for random characters acting as if it were a Java source file. It is the premise that is flawed (lack of (and impossibility of) proof by the language), which makes the rest of the statement moot.

To draw a less abstract example, some would argue that there is no point to this method:

but that is only because we are "pretty sure" (sure enough) that the Object constructor will have no "world state observable effect". Examples of what are considered "world state observations" are:
- files
- databases
- network sockets
I strongly disagree with this (that the notion of "world state observations" are external to the observer), but I'll go with it anyway since I am a relative minority in the world of language theory in this respect.

If the Object constructor wrote to a file, then you would argue that there *is* a point to that method, right? Of course! Or if it wrote to a database? What about what many call "the singleton design pattern" (a failed attempt to emulate world state)? In all these cases, you would say that there was a point to calling this constructor and ignoring the return value. However, the Object constructor (probably but not definitely) doesn't do any of these things, therefore, we are free to conclude that the method above is pointless.

In fact, it is theoretically possible to change the Object constructor to behave in exactly this way - so what now? Well, since Sun makes the guarantee - in English (read: javadoc) - that this is not going to happen, we accept it and hope that they keep their promise. If history is any indicator, Sun's promises aren't all that well kept, so instead we reason that Sun have no good reason to change the Object constructor this way and so we should be "pretty confident" that this is not going to happen. If it does, we are in pretty big trouble (high cost), but the chance of it occurring is low (low probability). This is how we might perform a cost/benefit analysis of the use of the Object constructor - even if in a less formal or ad hoc manner.

In contrast, there are languages that will fail to compile if the constructor/method were to have a world state observable effect occur. Ultimately, this simply means that assignment (e.g. x=y) is not permitted. These languages are called pure functional programming (pure FP) languages and I believe (wishful thinking?) that they are the successor to this (horrible) world of imperative programming in "OO" languages (excuse my bias). The downside is that they mandate a level of skill, mathematical grounding and cognitive ability that I believe exceeds the ability of most "Java programmers" and so the possibility of mainstream is a way off. Back to the point...

In conclusion, since your function/method makes no guarantees that it will not effect any change on world state (file, database, "singleton", etc.), then it is incorrect to conclude that "there is no point in ignoring the return value". If it made "some kind of guarantee but without proof", then you might conclude that "it is almost certainly not ok to ignore the return value". What you cannot do, is arrive at a point where you say with absoluteness that "it is definitely not ok to ignore the return value" since you have no proof to that effect.

I hope this makes sense and I am happy to answer any more questions that you might have on the topic.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cost/benefit analysis of the use of the Object constructor
  • Poster gave the cost/benifit: Java don't care.
  • Minor errata in Java VM coding effort explored by cs posters.
  • Failure modes explored: Java don't care.
  • Semantics of quick, short code samples demostrated for visitors.

  •  
    Justin Fox
    Ranch Hand
    Posts: 802
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    i dont understand what the problem is....

    if you have a void type, you dont return anything,

    and if you do, int/String/double/etc....

    you do?..

    but, can't you return 0; in a void function (such as main)
    if you want to?

    i know you can just return;

    but i think i saw return 0; in a java void class/funtion before..

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