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 › Returning multiple values from a function
Page Index Toggle Pages: 1
Returning multiple values from a function (Read 423 times)
Returning multiple values from a function
Sep 22nd, 2006, 8:57am
 
I've read of several different methods for returning multiple values from a function, but I'm having trouble getting them to work.  Essentially what I want to do is pass something like a point into the function and get another point out.
//not actual code, just an example
void draw()
{
 int x;
 int y;
 function(x,y);
 println(x);
 println(y0;
}

int function(int a, int b){
 a++;
 b++;
 return a; //yes I know this doesn't work, but you get what I mean
 return b;
}

Any help would be extremely useful.

Re: Returning multiple values from a function
Reply #1 - Sep 22nd, 2006, 10:23am
 
you could do something like this:

Code:
int[] function(int a, int b){ 
int[] temp = new int[2];
temp[0] = a++;
temp[1] = b++;
return temp;
}


and then have whatever function reference the values out of the array.
Re: Returning multiple values from a function
Reply #2 - Sep 22nd, 2006, 12:19pm
 
This is what objects are for. Create a new class which forms a compound of different variables/attributes and then let functions operate on the class instances:

Code:
class Pair {
int a;
int b;

Pair(int a, int b) {
this.a=a;
this.b=b;
}

void increment() {
a++;
b++;
}
}

Pair p;

void setup() {
p=new Pair(1,100);
}

void draw() {
p.increment();
println(p.a+" "+p.b);
}


hth!
Page Index Toggle Pages: 1