Simple concatenation (to trigegr a random fonction)

Hello everyone.

I'm struggling with some functions in my code!

I have some basics knowledge of programing but not much on C and arduino.

I have a TFT touchscreen, and a menu systeme on it.

With every button i trigger a specific fonction. What i'd like to do is having my last button to trigger a random a fonction.

I have 36 fonctions, named like this : m1b1action (menu1 button 2), m1b2action(), m2b3action()... etc From m1b1 to m6b6.

I want to do something like that (i know this is wrong but you get the point) :

rand_num_a = random(1,6);
rand_num_b = random(1,6);
rand_function = "m" . rand_num_a . "b"  rand_num_b . "action";

And execute that fonction.

This is what i have done so far, but it's awfull . it works but it's ugly :p

void m6b6action() {// random

  int rand = random(1,36);

  switch (rand) {
    case 1:
      m1b1action();
      break;
    case 2:
      m1b2action();
      break;
          case 3:
      m1b3action();
      break;
    case 4:
      m1b4action();
      break;
          case 5:
      m1b5action();
      break;
    case 6:
      m1b6action();
      break;
          case 7:
      m2b1action();
      break;
    case 8:
      m2b2action();
      break;
          case 9:
      m2b3action();
      break;
    case 10:
      m2b4action();
      break;
          case 11:
      m2b5action();
      break;
    case 12:
      m2b6action();
      break;
          case 13:
      m3b1action();
      break;
    case 14:
      m3b2action();
      break;
          case 15:
      m3b3action();
      break;
    case 16:
      m3b4action();
      break;
          case 17:
      m3b5action();
      break;
    case 18:
      m3b6action();
      break;
          case 19:
      m4b1action();
      break;
    case 20:
      m4b2action();
      break;
                case 21:
      m4b3action();
      break;
    case 22:
      m4b4action();
      break;
          case 23:
      m4b5action();
      break;
    case 24:
      m4b6action();
      break;
          case 25:
      m5b1action();
      break;
    case 26:
      m5b2action();
      break;
          case 27:
      m5b3action();
      break;
    case 28:
      m5b4action();
      break;
          case 29:
      m5b5action();
      break;
    case 30:
      m5b6action();
      break;
          case 31:
      m6b1action();
      break;
    case 32:
      m6b2action();
      break;
          case 33:
      m6b3action();
      break;
          case 34:
      m6b4action();
      break;
          case 35:
      m6b5action();
      break;
          case 36:
      m6b6action();
      break;

  }

Thanks in advance for your help :)

Ben.

Edit: Misunderstood, see the below answer.

What you are trying to accomplish is usually done in C with an array of function pointers, one entry for each of the functions. You can select a random index into the array and call the function through the pointer.

Read up on "function pointer table in C" and you'll get the idea.

With that many functions, it will be worth your while to learn how to put the table of function pointers into PROGMEM, as well.

Good luck with your project,

-br

You can get those things concatenated into one word if you want but you can't use it to call any function. At runtime all the function names are gone. Those functions have addresses and that's it. The names are meaningless. The board can look at your new concatenated string, but there's not going to be any function with that name anymore nor any way to call a function like that.

Best would be an array of function pointers and then call a random element of that array.

Thank you guys. I'll look into that.