Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Function Call

 
babu sharath
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check below code :

public class Hello
{
public static void main(String args[])
{ new Hello().doit(20);
}
static int doit(long t) {
System.out.println(t);
return t;
}
}

Above we are calling "doit" which is returning int , but in function call we are not receiving anywhere ( like int a = new Hello().doit(20); )
Above is compiles and runs fine .
Is it not required to receive the return value ??

Regards,
Sharath.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by babu sharath:
...Is it not required to receive the return value ?? ...

No, it's not required. As explained by Bruce Eckel in Thinking in Java...
...you can also call a method and ignore the return value. This is often referred to as calling a method for its side effect, since you don�t care about the return value, but instead want the other effects of the method call.
 
Murali Mohan
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


As far my knowledge the above class will not compile.
You can't return a long here. You should return an int.
 
Vijay Raj
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is he returning a long.

regards,
vijay.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Murali Mohan:

As far my knowledge the above class will not compile.
You can't return a long here. You should return an int.


Yes, good catch! It should be "return (int)t;" to avoid the loss of precision compiler error.
[ July 19, 2006: Message edited by: Barry Gaunt ]
 
Chaitanya Vivek
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code will not compile not because we are not collecting return value but because its actually returning a long instead of int

thanks
chaitanya
 
Ajit Amitav Das
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yea to my knowledge it won't compile as we are returning a long value in return statement and in method it's return type is int , we can downcast the value but if the expression which is returning is constant or evaluated in compile time or the value we are returning fits into the type of returntype in method defination. For more info in this go tojavaranch journal.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic