Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialFEI LI
828 Pointswhat's wrong with this code..
I am also confused about the term "return" , why return? return to where? when to return? when do we use void instead of return?
namespace Treehouse.CodeChallenges
{
class Frog
{
private int _numFliesEaten;
public void GetNumFliesEaten()
{ return _numFliesEaten;
}
public void SetNumFliesEaten(value)
{ _numFliesEaten = value;
}
}
}
1 Answer
Simon McGuirk
2,673 PointsHi Fei Li,
You use the return keyword when you want a method to return something when you call the method however in order for the method to be able to return something you need to specify what type of object you will return in the method declaration.
So in your example above you would need to set the return type for GetNumFliesEaten()
to int as we know that you are returning _numFliesEaten
and that it is of type int
public int GetNumFliesEaten()
{
return _numFliesEaten;
}
So if _numFliesEaten
is set to 4, if I call GetNumFliesEaten(), it will return the value 4.
For methods where we don't need to return an object, we set the return type to void:
public void SetNumFliesEaten(value)
{
_numFliesEaten = value;
}
Hope this helps