Functions argument

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;
    }
}

how can I return both a and b?

1 Like

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 };
}
4 Likes

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;
  }
}

3 Likes

Another option is to simply declare variables a & b in the global context. :globe_with_meridians:
Which in Java terms they’d be known as fields. :coffee:

int a, b;

void example(final char ch) {
  if (ch == 'y') {
    a *= b;
    b = (a + 5) * (b - 2);
  } else {
    b /= a;
    a += b;
  }
}
3 Likes

Or even better, use it as an actual full-fledged class, w/ constructors, fields & methods: :triumph:

// https://Discourse.Processing.org/t/functions-argument/11010/5
// GoToLoop (v1.1) (2019/May/09)

final Pair pair = new Pair(5, -10);

void setup() {
  println(pair);             // [ 5, -10 ]
  println(pair.calc('y'));   // [ -50, 540 ]
  println(pair.set(5, -10)); // [ 5, -10 ]
  println(pair.calc('u'));   // [ 3, -2 ]
  println(pair.clone());     // [ 3, -2 ]
  exit();
}

static final class Pair implements Cloneable {
  int a, b;

  Pair() {
  }

  Pair(final int a, final int b) {
    set(a, b);
  }

  Pair set(final int aa, final int bb) {
    a = aa;
    b = bb;
    return this;
  }

  Pair calc(final char ch) {
    if (ch == 'y') {
      a *= b;
      b = (a + 5) * (b - 2);
    } else {
      b /= a;
      a += b;
    }
    return this;
  }

  @Override Pair clone() {
    try {
      return (Pair) super.clone();
    }
    catch (final CloneNotSupportedException e) {
      throw new RuntimeException(e);
    }
  }

  @Override String toString() {
    return "[ " + a + ", " + b + " ]";
  }
}
4 Likes

Ok, yes I guess it’s a bit “backwards” not using classes fully.

Still, as for the class as kind-of-struct - method, I just realized it’s simpler without a constructor.

Pair test;

void setup() {
  test = new Pair();
  test = doubleHalf(3);
  //test.a = 34;
  //test.b = 5.5;

  println("Result : " + test.a + " and " + test.b);
  exit();
}


Pair doubleHalf(int val) {
  Pair temp = new Pair();
  temp.a = val * 2;
  temp.b = val / 2.0;
  return temp;
}


class Pair {
  int a;
  float b;
}