# Returning multiple values and also a problem with the class

**URL:** <https://discourse.processing.org/t/returning-multiple-values-and-also-a-problem-with-the-class/16821>\
**Category:** Beginners\
**Created:** [January 2, 2020, 8:18pm UTC](https://discourse.processing.org/t/returning-multiple-values-and-also-a-problem-with-the-class/16821 "2020-01-02T20:18:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ousmm](https://avatars.discourse-cdn.com/v4/letter/o/c4cdca/32.png) [@ousmm](https://discourse.processing.org/u/ousmm)\
**Post date:** [January 2, 2020, 8:18pm UTC](https://discourse.processing.org/t/returning-multiple-values-and-also-a-problem-with-the-class/16821/1 "2020-01-02T20:18:05Z")

</div>

```auto
void setup() {
}

float discriminant(float a, float b,float c ){
  float x1,x2;
  float d=(b*b)-4*a*c;
  if (d>0){
    x1=((-b)-sqrt(d))/2*a;
    x2=((-b)+sqrt(d))/2*a;
    return(x1) ;
          }
  if (d<0){
    x1=-b/2*a;
    return(x1);
  }
  else{
    println("pas de solution");
  }
    
}

```

i’m trying to make a program that calculates the solution of an equation of the second order , in the first case i want to return both x1 and x2 but apparently i cant .  
And also if there is no solution i want the function to say with a string that there is no solutions this is killing me help pls

---

<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:** [January 2, 2020, 9:04pm UTC](https://discourse.processing.org/t/returning-multiple-values-and-also-a-problem-with-the-class/16821/2 "2020-01-02T21:04:22Z")

</div>

> [@ousmm](#):
>
> , in the first case I want to return both _x1_ and _x2_, but apparently I can’t.

You can make your function to return a `float[]` array containing both values:

> **[Array / Reference](https://processing.org/reference/Array.html)**
>
> An array is a list of data. It is possible to have an array of any type of data. Each piece of data in an array is identified by an index number representing its position in the array. The first eleme…

> [@ousmm](#):
>
> … I want the function to say with a String that there is no solutions…

If the function returns a `float[]`, we can’t return a String or anything else.

However, we can return a `float[]` filled w/ 2 Float.NaN constants instead:  
[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Float.html#NaN](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Float.html#NaN)

```auto
/**
 * Bhaskara Akaria Quadratic (v1.0)
 * GoToLoop (2020/Jan/02)
 *
 * https://Discourse.Processing.org/t/
 * returning-multiple-values-and-also-a-problem-with-the-class/16821/2
 */

static final float[] discriminant(final float a, final float b, final float c) {
  final float d = b*b - 4*a*c;

  if (d < 0) return new float[] { Float.NaN, Float.NaN };

  final float sqrDelta = sqrt(d), a2 = 2*a;
  final float x1 = ( -b + sqrDelta) / a2;

  if (d == 0) return new float[] { x1, x1 };

  final float x2 = ( -b - sqrDelta) / a2;
  return new float[] { x1, x2 };
}

```
