I'm working on a snake game in java, and I made a "void" function inside a snake class, that returns if the snake eats the food. when trying to execute, the console shows me "Void methods cannot return a value". how could you return a "true" or "false" inside the "void" function?
Thanks for any help! Alon.
By definition, void
means that the function doesn't return anything. You can still return;
(with no value) to end the function early, but you can't return with a value.
To return a boolean
, change void
to boolean
.