We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › function returning true or false
Page Index Toggle Pages: 1
function returning true or false (Read 562 times)
function returning true or false
Jan 31st, 2006, 9:47pm
 
Hi!

I always get an error with the following code:

boolean pressed() {
   if (mousePressed == true && mouseX < x) {
     return true;
   }
}

Processing always says

"The method 'boolean pressed();' must contain a return statement with an expression compatible with type 'boolean'"

but true would be a compatible type in my opinion.

So what's wrong with my code?


controller
Re: function returning true or false
Reply #1 - Jan 31st, 2006, 9:59pm
 
Just a hint: what does the funtion return, if the if-condition is false?
Re: function returning true or false
Reply #2 - Feb 1st, 2006, 2:00am
 
Or if you want to be slick about it:

boolean pressed() {
 return mousePressed && mouseX < x;
}
Re: function returning true or false
Reply #3 - Feb 1st, 2006, 2:59am
 
controller,
cleverness aside, your return statement needs to be outside of the nested if.
Re: function returning true or false
Reply #4 - Feb 1st, 2006, 8:50am
 
Alternatively, you could include an else statement.

Either way the idea is this.. if a function has a return.. it MUST return in all cases, and in your situation.. there would be NO return if the condition is false.

So:
Add an else statement
or
Put it outside the conditional
or
Do it the "clever" way :]
Page Index Toggle Pages: 1