Loading...
Logo
Processing Forum

How to make a function return more than one variable?

Answered
  • Need more info
  • Answered
  • Working on it
in Programming Questions  •  2 years ago  
Hi. I'm new to processing so sorry if this is a silly question, but i havent managed to find the answer...

I need to write a function that returns more than one variable. How is that done in processing?

for example, i have

Copy code
  1. int dosomething(inputtype inputname){
  2.       int outputname;
  3.       // some calculation is done here and the output is outputname
  4.       return(outputname);
  5. }
Now, lets say i want the function to return two values, lets say an integer and a string. Is this possible?

Replies(9)

Not directly...  but it is possible to return an object that contains several values.  An obvious example is to use an array.  This could only be of a single type, but it's not too difficult to convert a number stored in a String array to an int.  Alternatively you could write a class that has int and string fields...
Here's an example of such a class:

Copy code
  1. void setup() {
  2.   size(200,200);
  3.   MyContainer foo = getStringAndInt();
  4.   println(foo.number + " " + foo.word);
  5. }

  6. // here's a function that returns a class object
  7. MyContainer getStringAndInt() {
  8.   return new MyContainer(3, "little pigs");
  9. }

  10. // the Class that contains the two variable types
  11. class MyContainer {
  12.  int number;
  13.  String word;
  14.  
  15.  MyContainer(int number, String word) {
  16.     this.number = number;
  17.     this.word = word;
  18.  }
  19.  // in strict OOP you'd probably add getter methods here,
  20.  // but in Processing this isn't strictly necessary
  21. }
Writing a class like this simply to store values can often be a simpler solution than relying on arrays: it avoids the need to convert variables to the appropriate type and you can reference them directly with their field name, which makes code easier to read than looking at an array[index] reference...
Thank you for answering so quickly and concisely. Thats just what I needed...
What if you want it to return two float variables?
Well, here goes a silly useless example:

void setup(){


//and to use

println(doubleFloats(10,20)[0] +"   "+ doubleFloats(10,20)[1]);
}



float[] doubleFloats(float a, float b)
{
      float[] results= new float[2];
      results[0] = a*2;
      results[1] = b*2;
      return results;

}


I would use an ArrayList as follows:

Copy code
  1. ArrayList testing() {
  2.   ArrayList returnedObject = new ArrayList();
  3.   returnedObject.add( (String) "a" );
  4.   returnedObject.add( (Integer) 666 );
  5.   returnedObject.add( (PVector) new PVector(2, 2) );

  6.   return returnedObject;
  7. }

  8. void setup() {
  9.   ArrayList justChecking = testing();
  10.   println(justChecking.get(0));
  11.   println(justChecking.get(1));
  12.   println(justChecking.get(2));

  13.   // just to be safe, you can also enforce the type by casting the values
  14.   println((String) justChecking.get(0));
  15.   println((Integer) justChecking.get(1));
  16.   println((PVector) justChecking.get(2));
  17. }

Or how to make Java an untyped (thus unsafe, prone to error) language... Perhaps convenient as you don't have to create a class, but not very practical will all these casts.
True. Call it a python infirmity...
 
Returning multiple values is usually done by packing them into tuple. Java supports generics, so there must be some generic tuple libraries - try to google them. That will probably not be very elegant (because of Java syntax), but still better than writing class each time or using untyped ArrayList.