Hi how can I return two values from a void function? I have already used c++ and there you could use & in these situations. Is there something like this in processing?
void example (char c, int a, int b){
if(c=='y'){
a=a*b;
b=(a+5)(b-2);
} else {
b=b/a;
a=a+b;
}
}
Given parameters a & b are of datatype int, you can simply return an int[] array:
static final int[] example(final char ch, int a, int b) {
if (ch == 'y') {
a *= b;
b = (a + 5) * (b - 2);
} else {
b /= a;
a += b;
}
return new int[] { a, b };
}
You could also use a class kind of like a struct. That way you could also return different types of variables. But if they are of the same type, an array is probably more efficient.
PairOfValues test;
//PairOfValues test = new PairOfValues(5, 23.5);
void setup() {
test = doubleHalf(3);
println("Result : " + test.a + " and " + test.b);
exit();
}
PairOfValues doubleHalf(int val) {
int nr1 = val * 2;
float nr2 = val / 2.0;
return new PairOfValues(nr1, nr2);
}
final class PairOfValues {
int a;
float b;
PairOfValues(int nr1, float nr2) {
this.a = nr1;
this.b = nr2;
}
}