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 › How to return a float value out of a void draw()
Page Index Toggle Pages: 1
How to return a float value out of a void draw() ? (Read 764 times)
How to return a float value out of a void draw() ?
Dec 3rd, 2009, 4:10pm
 
Hallo, maybe somone have an idea how to return the "toReturn" value out of draw func. into a void "ShapetoDraw" ? ....and then it should go back to draw func.

this is a code : (I used ControlP5 as a Panel to change a shape of primitives)




import controlP5.*;

ControlP5 controlP5;
ControlWindow controlWindow;

int nr;

void setup(){
 size(400,400);

 controlP5 = new ControlP5(this);
 controlP5.hide();
 controlWindow = controlP5.addControlWindow("Control Shape Panel",250,400);

 Radio r = controlP5.addRadio("radio",25,225);
 r.setWindow(controlWindow);
 r.add("1",1);
 r.add("2",2);


}

void draw(){
 background(0);

 ShapetoDraw(nr);

 for(int i=0;i<20;i++){

   toReturn = 300/i;

 }

}

void ShapetoDraw(int nr){

 if(nr==1){
   ellipse(100,100,toReturn,toReturn);
 }

 if(nr==2){
   rect(100,100,toReturn,toReturn);
 }

}


void radio(int theID) {
 switch(theID) {
   case(1):
   nr = 1;  
   break;  
 }

 switch(theID) {
   case(2):
   nr = 2;  
   break;  
 }

}



...thanks for all help Smiley
Re: How to return a float value out of a void draw() ?
Reply #1 - Dec 3rd, 2009, 9:28pm
 
I am not sure if I understand the question. Is this your whole code?
cause you are passing variables like theID and "nr" to your functions, but where do you set these values?

cause thats actually the way how you pass a value to a function.
on the other side you dont need to pass it back to draw as it is accessible there anyway, also when changend.

but i dont really get what you are trying to do. for example your for loop doesnt make sense if you dont do anything in there except changing a variable. what you do at the moment is not much more than setting toReturn to 15.

so maybe you wanna do something like this ?



for(int i=0;i<20;i++){

  toReturn = 300/i;
ShapetoDraw(toReturn);
}

cause in this case you would draw different sized, shapes. even nr is not used and you are drawing them at the same position but i hope you understand what i mean.

Re: How to return a float value out of a void draw() ?
Reply #2 - Dec 4th, 2009, 12:28am
 
As Cedric said, tell us what you want to do, otherwise we can't help that much since the code makes no sense.

Maybe the following can help, anyway :

a function can expect more than one argument :

Code:
void ShapeToDraw(int x, int y) {
 ellipse(x, y, 10, 10);
}

ShapeToDraw(10, nr);


a function can return something (change the "void" keyword into the thing you want to return) :

Code:
float ShapeToDraw(int x, int y) {
 ellipse(x, y, 10, 10);
return x/y;
}


but the draw() function is void, and will never return anything.
Re: How to return a float value out of a void draw() ?
Reply #3 - Dec 4th, 2009, 1:34am
 
To clarify and add emphasis: you don't call yourself draw(), so you will never get a return value (which it just doesn't have).

All you can do is to have some side effect, like changing the values of global variables (like your own nr).
Which is, somehow, more powerful (you can change as many variables as you want).
Page Index Toggle Pages: 1