# How to use switch() in a custom function?

**URL:** <https://discourse.processing.org/t/how-to-use-switch-in-a-custom-function/19753>\
**Category:** Beginners\
**Created:** [April 13, 2020, 3:53pm UTC](https://discourse.processing.org/t/how-to-use-switch-in-a-custom-function/19753 "2020-04-13T15:53:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![codebodger](https://avatars.discourse-cdn.com/v4/letter/c/a9adbd/32.png) [@codebodger](https://discourse.processing.org/u/codebodger)\
**Post date:** [April 13, 2020, 3:53pm UTC](https://discourse.processing.org/t/how-to-use-switch-in-a-custom-function/19753/1 "2020-04-13T15:53:41Z")

</div>

I need to create a function called `tpc`, taking in one integer argument, and returning an integer based on a switch function. I have already written the switch function, and it is quite simple. The `return` function automatically breaks out of `tpc()` and so I don’t need to use `break;`

So far this is what I have:

```auto
int tpc(int input) {
  switch(input % 12) {
    case 0:
      return 22;
    case 1:
      return 17;
    case 2:
      return 24;
    case 3:
      return 19;
    case 4:
      return 14;
    case 5:
      return 21;
    case 6:
      return 16;
    case 7:
      return 23;
    case 8:
      return 18;
    case 9:
      return 13;
    case 10:
      return 20;
    case 11:
      return 15;
  }
}

```

It gives me an error in line 1 saying `This method must return a result of type int`

I have no idea how to fix this, so I thought I would post it here.

Thanks in advance.

---

<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:** [April 13, 2020, 4:03pm UTC](https://discourse.processing.org/t/how-to-use-switch-in-a-custom-function/19753/2 "2020-04-13T16:03:25Z")

</div>

You can use the `default` option w/ `return` inside `switch ()`:  
[Processing.org/reference/default.html](http://Processing.org/reference/default.html)

Or simply have a `return` outside your `switch ()` block.

You can choose some arbitrary value for it, such as `-1`.

---

<div class="post-metadata">

**Author:** ![codebodger](https://avatars.discourse-cdn.com/v4/letter/c/a9adbd/32.png) [@codebodger](https://discourse.processing.org/u/codebodger)\
**Post date:** [April 13, 2020, 4:11pm UTC](https://discourse.processing.org/t/how-to-use-switch-in-a-custom-function/19753/3 "2020-04-13T16:11:32Z")

</div>

I do not quite understand what you are saying. It seems that you mean that I could asign a variable to hold the return value until I close the switch statement and return that instead afterwards, however the variable would be local, and so it would no longer exist after the switch statement had closed.

---

<div class="post-metadata">

**Author:** ![codebodger](https://avatars.discourse-cdn.com/v4/letter/c/a9adbd/32.png) [@codebodger](https://discourse.processing.org/u/codebodger)\
**Post date:** [April 13, 2020, 4:16pm UTC](https://discourse.processing.org/t/how-to-use-switch-in-a-custom-function/19753/4 "2020-04-13T16:16:37Z")

</div>

Use an array:

```auto
int tpc(int dVal) {
  int[] output = new int[1];
  switch(dVal % 12) {
    case 0:
      output[0] = 22;
      break;
    case 1:
      output[0] = 17;
      break;
    case 2:
      output[0] = 24;
      break;
    case 3:
      output[0] = 19;
      break;
    case 4:
      output[0] = 14;
      break;
    case 5:
      output[0] = 21;
      break;
    case 6:
      output[0] = 16;
      break;
    case 7:
      output[0] = 23;
      break;
    case 8:
      output[0] = 18;
      break;
    case 9:
      output[0] = 13;
      break;
    case 10:
      output[0] = 20;
      break;
    case 11:
      output[0] = 15;
      break;
  }
  return output[0];
}

```

---

<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:** [April 13, 2020, 4:20pm UTC](https://discourse.processing.org/t/how-to-use-switch-in-a-custom-function/19753/5 "2020-04-13T16:20:44Z")

</div>

> [@codebodger](#):
>
> It gives me an error in line 1 saying `This method must return a result of type int`

- When we get such `return` error, it means there’s a chance the function may not return any value.
- Your **tcp()** function states that it must return an `int` value: `int tpc(int input) {`
- However, your `switch ()` block doesn’t cover all cases, and it can fail to `return` an `int`.
- Using `default` w/ a `return` would cover all cases though:

```auto
static final int tpc(final int value) {
  switch(abs(value) % 12) {
  case 0: 
    return 22;
  case 1:
    return 17;
  case 2:
    return 24;
  case 3:
    return 19;
  case 4:
    return 14;
  case 5:
    return 21;
  case 6:
    return 16;
  case 7:
    return 23;
  case 8:
    return 18;
  case 9:
    return 13;
  case 10:
    return 20;
  case 11:
    return 15;
  default: 
    return -1;
  }
}

```

Given all your `case` values is a sequence from 0 to 15, you can use a pre-created array in place of a `switch ()` block:

```auto
static final byte[] TPC = {
  22, 17, 24, 19, 14, 21, 16, 23, 18, 13, 20, 15
};

static final int tpc(final int value) {
  return TPC[abs(value) % 12];
}

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [April 14, 2020, 9:40pm UTC](https://discourse.processing.org/t/how-to-use-switch-in-a-custom-function/19753/6 "2020-04-14T21:40:54Z")

</div>

@codebodger – the int[] array doesn’t matter in the code you marked as a solution. What matters in that is that you need to either include a default return or include a return after your switch statement ends. Once you do that, the error message condition is satisfied – the function will always return.
