FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   returning booleans from a method
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: returning booleans from a method  (Read 517 times)
mKoser

WWW Email
returning booleans from a method
« on: May 23rd, 2003, 3:43pm »

I am struggeling with some code... it's part of a bigger project (creating some custom P5-methods for communicating with some custom-build circuit-boards... i'll post it all when the project is finished!)
 
When I create a method to return a boolean value, I recieve an error saying that the Method needs to return a value.
 
Here is my code (as simplified as I could get it):
 
// start of code
 
void setup(){
  size(200, 200);
}
 
void loop(){
  checkNum(5);
}
 
boolean checkNum(int num){  
  if(num = 5){
    return true;
    println("number " + num + " was passed in");
  }else{
    return false;
  }
}
 
//end of code
 
I've gone through my Java-book and looked at java.sun.com and I just can't see what I am doing wrong!
 
The strange thing is, when I do something like this:
 
boolean sendStateBack(){
  return false;
}
 
I get no complaints... but as soon as I place the return command within an if-statement or have the return statement return a variable (containing a boolean value) I get the error!
 

mikkel crone koser | www.beyondthree.com | http://processing.beyondthree.com
arielm

WWW
Re: returning booleans from a method
« Reply #1 on: May 23rd, 2003, 3:59pm »

i think the problem is the "println()", which is placed after "return" (java, unlike javascript, don't like code placed after a "return" in the same block... and also, because your function declaration states that something is to be returned, every potential branch of code in it should always end with a "return")
 
not related to the current JLS error: i think you meant "num == 5" instead of "num = 5"...
 

Ariel Malka | www.chronotext.org
mKoser

WWW Email
Re: returning booleans from a method
« Reply #2 on: May 23rd, 2003, 4:32pm »

Thank you for the quick reply, and thank you for explaining the bit about ending each branch with a return-statement
 
The example I posted ealier works fine now - when applying my newly gained knowledge to some of my more advanced boolean-method tests, I am still having some trouble!
 
Below I have pasted some more dummy code - I am still getting an error saying the Method needs to return something.  
 
//start code
byte[] pins = {3, 7, 21, 42, 102};
boolean[] states = {false, false, false, true, true};
 
int ranNum;
 
void setup(){
  size(200, 200);
}
 
void loop(){
  // pick a random number in array
  ranNum = (int)random(5);
  println("pin number : " + pins[ranNum] + " is " + getButtonState(ranNum));
 
  delay(200);
}
 
boolean getButtonState(int pin){
  // look through pins-array
  for(int i = 0; i < pins.length; i++){
 
    // if the passed-in number exist in the array, return it's state
    if((int)inputPins[i] == pin){
 return inPinStates[i];
    }
  }
}
//end code
 
A brief look at this from some of you wiz's out there (arielm?) would be greatly appriciated!
 

mikkel crone koser | www.beyondthree.com | http://processing.beyondthree.com
arielm

WWW
Re: returning booleans from a method
« Reply #3 on: May 23rd, 2003, 4:59pm »

the error is still related to the "every potential branch of code" ending:
 
i mean the java compiler is so... that he looks into your "for" loop and says: what if the "if" is never reached? then you have a "potential" branch of code that ends without a "return"!
 
so one solution is to add "return false;" after the loop...
 

Ariel Malka | www.chronotext.org
mKoser

WWW Email
Re: returning booleans from a method
« Reply #4 on: May 23rd, 2003, 5:19pm »

Ahhh! I see what you mean!
Got it fixed now, thanks alot!
*m
 

mikkel crone koser | www.beyondthree.com | http://processing.beyondthree.com
Pages: 1 

« Previous topic | Next topic »