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

Help in method return type

 
Erum Momin
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I'm having difficulty in understanding..... if a method is defined with some kind of return type other than void then is it necessary to assign the called method which has returned something to it's type specified in the method....
for eg.... take this method

class A{
public int aMethod(){ int i; i++; return i;}
}

should this be called like this....

A a = new A();
int j = a.aMethod();--------------------------->1

or like this
a.aMethod();------------------------------------>2

or you can call it any way you want???
According to me no.1 is correct but i've found in most of the mock tests saying that 2 works either..... you can call it any way you want... I have seen the questions where in they have called no.1 and no.2 together and answer is something different from compilation error.... Can anyone please explain it to me...
 
Henry Wong
author
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If a method has a return type, it is perfectly valid to ignore it. For example, say a method that does something and returns a status, shouldn't there be cases where you just want the something to happen (and at a best effort), and you don't care about the success status?

On the other hand, if the method doesn't return anything (void), then assigning it to something, or using it in an expression, should yield a compilation error.

Henry
 
Erum Momin
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry,
Actually i was confused since this does not happen in c or c++ and i was comparing it with that.... but now it's clear! ! !
Thank you.
 
Henry Wong
author
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually i was confused since this does not happen in c or c++ and i was comparing it with that


What "does not happen in c or c++"?

Henry
 
Erum Momin
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Actually i was confused since this does not happen in c or c++ and i was comparing it with that


What "does not happen in c or c++"?

Henry


i think ignoring the return type is not allowed in c or c++
 
Henry Wong
author
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Erum Momin wrote:
i think ignoring the return type is not allowed in c or c++


That's not true.... Heck, even printf() returns an int. It would be annoying to have to check the return type of calls to printf()...

Henry
 
Erum Momin
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmmm.... you are rite....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic