Loading...
Logo
Processing Forum
I'm learning processing with "Learning Processing - A Beginners Guide To Programming Images, Animation & Interaction".

I understood you use void when a function doesn't return a value, but in a lot of examples, the function DOES return a value and is still called "void", for example (p. 175):

void rollover(int mx, int my) {
  if (mx > x && mx < x + w) {
mouse = true; } else {
     mouse = false;
   }
} }

this returns a boolean variable "mouse", isn't it? How do I misunderstand this?

Kind regards,

John

Replies(5)

Don't confuse returning a value from a function with having a function set the value of a global variable.
If a function returns a value - that is, it doesn't have a return value of "void" - then it has to return a value using the function return(). The example function you posted (rollover()) may set the value of variable "mouse", but it doesn't return any value!

Consider:

Copy code
  1. int global_int = 0;

  2. void setup(){
  3.   size(200,200);
  4.   int setup_int;
  5.   setup_int = setGlobalAsTwoReturnThree();
  6.   println( "global_int is now" + global_int );
  7.   println( "setup_int is now" + setup_int );
  8. }

  9. int setGlobalAsTwoReturnThree(){
  10.   global_int = 2;
  11.   return( 3 );
  12. }
Thank you tfguy44

So, in your example, global_int is "set to 2" and the function setGlobalAsTwoReturnThree() "returned" "3".

Isn't the result the same? global_int = 2 and setup_int = 3

When should I use "void" and when a return-value?


I too was confused about "void" and "return" functions, but it's actually quite simple to get the hang of. Check this:
Copy code
  1. int a = 1;   
  2. int b = 2;
  3. int c = 3;
  4. int d = 4;   

  5. void setup() { 
  6. int sum1 = addNumbers(a,b);
  7. int sum2 = addNumbers(c,d);    
  8. output(sum1); 
  9. output(sum2);   
  10. }

  11. // function manipulates variables, sends back the result   
  12. int addNumbers(int num1, int num2){
  13. int result = num1 + num2;
  14. return (result);
  15. }

  16. // function writes to screen
  17. void output(int value) {
  18. println (value);    
  19. rect(20, value*10, 20, 20);
  20. }                                                             

In above sketch I created two custom functions, one returning a value (addNumbers) and one that has a bunch of commands but does not returning anything (output).

Use return functions whenever you want to manipulate variables, and want to use the resulting value at some later point in your sketch. Obviously, the manipulation/calculation part can be far more complex than a simple addition and the returned value can be an int, float, String, Array or any other datatype. It's just important, that you tell the function what datatype it's going to return, by using a keyword, in above case "int". Check out the reference for the use of return: Processing Reference: return

Whenever you just want to group some commands together for the sketch to execute in one swift go, then you put them in a void function. That's often the case, when you want to draw something to the stage. The "void" just says "This function will do stuff, but won't give you back a value to use in your sketch". See this example for a good explanation: Processing Learning: Functions

Now, someone more proficient at programming than me might have a better explanation of which type of function to use when and definitely have a more technical correct description. Still, I hope this helps a little.
• void says "do this"
 
• return value says "do this and tell me what you got"
 
So I would use void like this, say:
 
void display()
{
      //do some drawing
}
 
or:
 
void addOneToEachNumber(array someIntegers)
{
      for(int a = 0; a < someIntegers.length; a++)
      {
            someIntegers[a] ++;
      }
}
 
I could put exactly the same into a return value function, adding a return command:
 
int[] addOneToEachNumber(int[] someIntegers)
{
      for(int a = 0; a < someIntegers.length; a++)
      {
            someIntegers[a] ++;
      }
      return someIntegers;
}
 
but it's likely that I called the function, as tfguy44 did, by saying somewhere else in the code:
 
someNewArray = addOneToEachNumber(someIntegerArray);
 
because I want to set someNewArray to the result of doing this function on the someIntegerArray.
Thanks allonestring and eightohnine. I think I see the difference now.

see you later :-)