# Function Return Error

**URL:** <https://discourse.processing.org/t/function-return-error/29501>\
**Category:** Beginners\
**Created:** [April 19, 2021, 7:10pm UTC](https://discourse.processing.org/t/function-return-error/29501 "2021-04-19T19:10:29Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bromo](https://avatars.discourse-cdn.com/v4/letter/b/8491ac/32.png) [@Bromo](https://discourse.processing.org/u/Bromo)\
**Post date:** [April 19, 2021, 7:10pm UTC](https://discourse.processing.org/t/function-return-error/29501/1 "2021-04-19T19:10:29Z")

</div>

Hi, I’m pretty new with functions and was wondering what I could do differently here. Ideally I’d like the function to sort the two numbers given and give me the number that’s closest to 100 (without going over). Currently getting a “This method must return a result type of int” error.

```auto
void setup()
{
  println(functionName(18,25));
}

int functionName(int firstNumber, int secondNumber)
{
  if (firstNumber > 0 && secondNumber > 0 || (firstNumber < 100 && secondNumber < 100))
  {
    if (firstNumber >= secondNumber && firstNumber < 100)
    {
      return firstNumber;
    }
    else if (secondNumber >= firstNumber && secondNumber < 100)
    {
      return secondNumber;
    }
  }
  else
  {
    return 0;
  }
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 19, 2021, 7:23pm UTC](https://discourse.processing.org/t/function-return-error/29501/2 "2021-04-19T19:23:13Z")

</div>

Yeah.

**all** possible ways the functions can be executed must return something.

BUT the first if-clause contains some `if..else if...`-construct that doesn’t have an else block.

that’s the problem

result is 25 here by the way

Warm regards,

Chrisir

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 19, 2021, 7:24pm UTC](https://discourse.processing.org/t/function-return-error/29501/3 "2021-04-19T19:24:27Z")

</div>

Hey and welcome to the forum!

Great to have you here!

---

<div class="post-metadata">

**Author:** ![Bromo](https://avatars.discourse-cdn.com/v4/letter/b/8491ac/32.png) [@Bromo](https://discourse.processing.org/u/Bromo)\
**Post date:** [April 19, 2021, 7:28pm UTC](https://discourse.processing.org/t/function-return-error/29501/4 "2021-04-19T19:28:48Z")

</div>

Hmm okay, so I either need to add an `else` to the `else if` or somehow keep the parameters and delete the `if` that’s attached?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 19, 2021, 7:29pm UTC](https://discourse.processing.org/t/function-return-error/29501/5 "2021-04-19T19:29:01Z")

</div>

But there are other ways to do it. For example check the distance to 100 and then pick the number that is closer to 100 when it’s \<100

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 19, 2021, 7:30pm UTC](https://discourse.processing.org/t/function-return-error/29501/6 "2021-04-19T19:30:24Z")

</div>

> [@Bromo](#):
>
> add an `else` to the `else if`

That’s what I did

Chrisir

---

<div class="post-metadata">

**Author:** ![Bromo](https://avatars.discourse-cdn.com/v4/letter/b/8491ac/32.png) [@Bromo](https://discourse.processing.org/u/Bromo)\
**Post date:** [April 19, 2021, 7:37pm UTC](https://discourse.processing.org/t/function-return-error/29501/7 "2021-04-19T19:37:12Z")

</div>

Okay I’ll have to play around with it some more, thanks for the help!

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 19, 2021, 7:41pm UTC](https://discourse.processing.org/t/function-return-error/29501/8 "2021-04-19T19:41:35Z")

</div>

here is my version, haven’t tested with other numbers though

I made a description above

maybe you can use

```auto
if( abs(100 - a) < abs(100 - b) ) {
   // a is closer to 100
   if(a<100) 
      return a; 
   else if (b<100) return b; 
   else return 0; 
} else {
   // b is closer to 100
   .....
}

```

* * *

**My old version**

```auto

void setup() {
  size(300, 300); 
  println(functionName(18, 25));
}

int functionName(int firstNumber, int secondNumber) {
  if (firstNumber > 0 && secondNumber > 0 || (firstNumber < 100 && secondNumber < 100)) {
    if (firstNumber >= secondNumber && firstNumber < 100) {
      return firstNumber;
    } else if (secondNumber >= firstNumber && secondNumber < 100) {
      return secondNumber;
    } 
    //+++++++++++++++++++++++++++++++++
    else {
      return 0;
    }
    //+++++++++++++++++++++++++++++++++
  } else {
    return 0;
  }
}

```

---

<div class="post-metadata">

**Author:** ![Bromo](https://avatars.discourse-cdn.com/v4/letter/b/8491ac/32.png) [@Bromo](https://discourse.processing.org/u/Bromo)\
**Post date:** [April 19, 2021, 7:47pm UTC](https://discourse.processing.org/t/function-return-error/29501/9 "2021-04-19T19:47:38Z")

</div>

> [@Chrisir](#):
>
> `abs(100 - a)`

How does `abs` work? Does it just check if it’s a positive number?

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [April 19, 2021, 7:51pm UTC](https://discourse.processing.org/t/function-return-error/29501/10 "2021-04-19T19:51:19Z")

</div>

> [@Bromo](#):
>
> How does `abs` work? Does it just check if it’s a positive number?

This answer to this is available in the references.

See:  
_[https://processing.org/](https://processing.org/)_

`:)`

---

<div class="post-metadata">

**Author:** ![Bromo](https://avatars.discourse-cdn.com/v4/letter/b/8491ac/32.png) [@Bromo](https://discourse.processing.org/u/Bromo)\
**Post date:** [April 19, 2021, 7:59pm UTC](https://discourse.processing.org/t/function-return-error/29501/11 "2021-04-19T19:59:27Z")

</div>

> [@glv](#):
>
> See:  
> _[https://processing.org/](https://processing.org/)_

That’s super useful! Than you

---

<div class="post-metadata">

**Author:** ![Bromo](https://avatars.discourse-cdn.com/v4/letter/b/8491ac/32.png) [@Bromo](https://discourse.processing.org/u/Bromo)\
**Post date:** [April 19, 2021, 8:06pm UTC](https://discourse.processing.org/t/function-return-error/29501/12 "2021-04-19T20:06:40Z")

</div>

> [@Chrisir](#):
>
> here is my version,

This helped a ton, thanks again!
