# Functions argument

**URL:** <https://discourse.processing.org/t/functions-argument/11010>\
**Category:** Beginners\
**Created:** [May 7, 2019, 8:34pm UTC](https://discourse.processing.org/t/functions-argument/11010 "2019-05-07T20:34:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Reggi](https://avatars.discourse-cdn.com/v4/letter/r/a6a055/32.png) [@Reggi](https://discourse.processing.org/u/Reggi)\
**Post date:** [May 7, 2019, 8:34pm UTC](https://discourse.processing.org/t/functions-argument/11010/1 "2019-05-07T20:34:01Z")

</div>

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?

```auto
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?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [May 7, 2019, 8:44pm UTC](https://discourse.processing.org/t/functions-argument/11010/2 "2019-05-07T20:44:10Z")

</div>

Given parameters _a_ & _b_ are of datatype `int`, you can simply return an `int[]` array:

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

```

---

<div class="post-metadata">

**Author:** ![raron](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/raron/32/13651_2.png) [@raron](https://discourse.processing.org/u/raron)\
**Post date:** [May 9, 2019, 11:18am UTC](https://discourse.processing.org/t/functions-argument/11010/3 "2019-05-09T11:18:09Z")

</div>

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.

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

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [May 9, 2019, 12:25pm UTC](https://discourse.processing.org/t/functions-argument/11010/4 "2019-05-09T12:25:01Z")

</div>

Another option is to simply declare variables _a_ & _b_ in the global context. 🌐  
Which in Java terms they’d be known as fields. ☕

```auto
int a, b;

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

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [May 9, 2019, 12:42pm UTC](https://discourse.processing.org/t/functions-argument/11010/5 "2019-05-09T12:42:05Z")

</div>

> [@raron](#):
>
> You could also use a `class` kind of like a `struct`.

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

```auto
// 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 + "]";
  }
}

```

---

<div class="post-metadata">

**Author:** ![raron](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/raron/32/13651_2.png) [@raron](https://discourse.processing.org/u/raron)\
**Post date:** [May 12, 2019, 10:14am UTC](https://discourse.processing.org/t/functions-argument/11010/6 "2019-05-12T10:14:31Z")

</div>

> [@GoToLoop](#):
>
> Or even better, use it as an actual full-fledged `class` , w/ constructors, fields & methods: 😤

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.

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

```
