How to use switch() in a custom function?

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:

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.

1 Like

You can use the default option w/ return inside switch ():
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.

1 Like

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.

Use an array:

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];
}
  • 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:
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:

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];
}
2 Likes

@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.

1 Like