cancel
Showing results for 
Search instead for 
Did you mean: 

How to made a function to call other functions?

angeliran
Senior

Hi to all!!!

I do repeteadly this function:

switch(segundero_d)

{

case 0: numero_0(); break;

case 1: numero_1(); break;

case 2: numero_2(); break;

case 3: numero_3(); break;

case 4: numero_4(); break;

case 5: numero_5(); break;

case 6: numero_6(); break;

case 7: numero_7(); break;

case 8: numero_8(); break;

case 9: numero_9(); break;

}

switch(segundero)

{

case 0: numero_0(); break;

case 1: numero_1(); break;

case 2: numero_2(); break;

case 3: numero_3(); break;

case 4: numero_4(); break;

case 5: numero_5(); break;

case 6: numero_6(); break;

case 7: numero_7(); break;

case 8: numero_8(); break;

case 9: numero_9(); break;

}

...

where the function numero_x are:

void numero_0(void)

{l=m;

 fb[l][n]=0x3E; //b"00111110";

fb[l+1][n]=0x22; //b"00100010";

fb[l+2][n]=0x22; //b"00100010";

fb[l+3][n]=0x22; //b"00100010";

fb[l+4][n]=0x3E; //b"00111110";

n++;}

void numero_1(void)

{l=m;

 fb[l][n]=0x08; //b"00001000";

fb[l+1][n]=0x18; //b"00011000";

fb[l+2][n]=0x08; //b"00001000";

fb[l+3][n]=0x08; //b"00001000";

fb[l+4][n]=0x3E; //b"00111110";

n++;}

...

And i want to do this, with a function, to call the functions numero_x, to reduce my code, can somebody help me?

Thanx in advice!!!

EDIT: I added a pic, to show, that my code works, but my objetive, is to pulish the code, =)

1 ACCEPTED SOLUTION

Accepted Solutions
angeliran
Senior

In advance, I thank @hwa​ , @Bob S​ , @TDK​ , @S.Ma​  , for dedicating their valuable time, in answering something, which perhaps, from another perspective, was something simple.

Regardless, if I am an electronics engineer, and a couple of years ago, I actually took programming courses, or as @hwa​  comments, self-learning.

I would like to share with you the answer to my question, through which was the solution in the end:

  1. uint8_t tabla_de_valores[10][5];//MATRIZ 50, yes, keil, my compiler, only accept this way
  2.  
  3. uint8_t tabla_de_valores[][5]={
  4. //numero_0
  5. {
  6. 0x3E, //b"00111110"
  7. 0x22, //b"00100010"
  8. 0x22, //b"00100010"
  9. 0x22, //b"00100010"
  10. 0x3E, //b"00111110"
  11. },
  12.  
  13. //numero_1
  14. {
  15. 0x08, //b"00001000"
  16. 0x18, //b"00011000"
  17. 0x08, //b"00001000"
  18. 0x08, //b"00001000"
  19. 0x3E, //b"00111110"
  20. },
  21.  
  22. //numero_2
  23. {
  24. 0x3E, //b"00111110"
  25. 0x02, //b"00000010"
  26. 0x3E, //b"00111110"
  27. 0x20, //b"00100000"
  28. 0x3E, //b"00111110"
  29. },
  30.  
  31. //numero_3
  32. {
  33. 0x3E, //b"00111110"
  34. 0x02, //b"00000010"
  35. 0x3E, //b"00111110"
  36. 0x02, //b"00000010"
  37. 0x3E, //b"00111110"
  38. },
  39.  
  40. //numero_4
  41. {
  42. 0x22, //b"00100010"
  43. 0x22, //b"00100010"
  44. 0x3E, //b"00111110"
  45. 0x02, //b"00000010"
  46. 0x02, //b"00000010"
  47. },
  48. //numero_5
  49. {
  50. 0x3E, //b"00111110"
  51. 0x20, //b"00100000"
  52. 0x3E, //b"00111110"
  53. 0x02, //b"00000010"
  54. 0x3E, //b"00111110"
  55. },
  56.  
  57. //numero_6
  58. {
  59. 0x3E, //b"00111110"
  60. 0x20, //b"00100000"
  61. 0x3E, //b"00111110"
  62. 0x22, //b"00100010"
  63. 0x3E, //b"00111110"
  64. },
  65.  
  66. //numero_7
  67. {
  68. 0x3E, //b"00111110"
  69. 0x02, //b"00000010"
  70. 0x02, //b"00000010"
  71. 0x02, //b"00000010"
  72. 0x02, //b"00000010"
  73. },
  74.  
  75. //numero_8
  76. {
  77. 0x3E, //b"0011 1110"
  78. 0x22, //b"0010 0010"
  79. 0x3E, //b"0011 1110"
  80. 0x22, //b"0010 0010"
  81. 0x3E, //b"0011 1110"
  82. },
  83.  
  84. //numero_9
  85. {
  86. 0x3E, //b"0011 1110"
  87. 0x22, //b"0010 0010"
  88. 0x3E, //b"0011 1110"
  89. 0x02, //b"0000 0010"
  90. 0x02, //b"0000 0010"
  91. },
  92.  
  93.  
  94. };
  95.  
  96.  
  97. void numero_x(uint8_t select) 
  98. {
  99.   l = m;
  100.  
  101.   for (uint8_t interno = 0; interno < 5; interno++) {
  102.    fb[l+interno][n] = tabla_de_valores[select][interno];
  103.   }
  104.   n++;
  105. }

This go, where i need it:

  1. numero_x(horatero_d);
  2. numero_x(horatero);
  3. letra_dos_puntos(); // 15
  4. numero_x(minutero_d);
  5. numero_x(minutero);
  6. letra_dos_puntos();
  7. numero_x(segundero_d);
  8. numero_x(segundero); // 20

I hope it works for someone =)

P.D.: The next step, it´s to made with letters:

void letra_A(void){

l=m;

 fb[l][n]=0x1C; //b"00011100";

fb[l+1][n]=0x22; //b"00100010";

fb[l+2][n]=0x3E; //b"00111110";

fb[l+3][n]=0x22; //b"00100010";

fb[l+4][n]=0x22; //b"00100010";

n++;}

void letra_B(void)

{l=m;

 fb[l][n]=0x3C; //b"00111100";

fb[l+1][n]=0x22; //b"00100010";

fb[l+2][n]=0x3C; //b"00111100";

fb[l+3][n]=0x22; //b"00100010";

fb[l+4][n]=0x3C; //b"00111100";

n++;}

void letra_C(void)

{l=m;

 fb[l][n]=0x1E; //b"00011110";

fb[l+1][n]=0x20; //b"00100000";

fb[l+2][n]=0x20; //b"00100000";

fb[l+3][n]=0x20; //b"00100000";

fb[l+4][n]=0x1E; //b"00011110";

n++;}

View solution in original post

17 REPLIES 17
TDK
Guru

> And i want to do this, with a function, to call the functions numero_x,

Make an array of function pointers and put them in there.

void Function1() {
    printf("Function1\n");
}
void Function2() {
    printf("Function2\n");
}
void Function3() {
    printf("Function3\n");
}
 
typedef void (*VoidFunction)();
 
VoidFunction function[] = {
    Function1,
    Function2,
    Function3,
};
 
 
void call_function(int i) {
    function[i]();
}
 
int main()
{
 
    call_function(0);
    call_function(1);
    call_function(2);
 
    return 0;
}

If you feel a post has answered your question, please click "Accept as Solution".
S.Ma
Principal

Usually compiler in optimisation mode will replace switch case from an index with function array for you in assembly. What most compilers wont guess for you is create data tables. You could create like string arrays for exemple as a starting point.

Thx for answer! I want to make my code simpler to understand.

Thx for answer! I will try to do it!

angeliran
Senior

Ok, i´ve tried:

int temporal=0;

typedef void (*escoger_numero_vector)();

escoger_numero_vector escoger_numero [int temporal] = 

{

switch(temporal)

{

case 0: numero_0; break, //numero_x are functions, where x is numbers from 0 to 9

case 1: numero_1; break;

case 2: numero_2; break;

case 3: numero_3; break;

case 4: numero_4; break;

case 5: numero_5; break;

case 6: numero_6; break;

case 7: numero_7; break;

case 8: numero_8; break;

case 9: numero_9; break;

}

};

But keil give to me this errors:

../Src/main.c(781): error: #29: expected an expression

 escoger_numero_vector escoger_numero [int temporal] = 

../Src/main.c(784): error: #29: expected an expression

    switch(temporal)

../Src/main.c(796): warning: #12-D: parsing restarts here after previous syntax error

        },

../Src/main.c: 1 warning, 2 errors

Bob S
Principal

Go back and look at @TDK​ 's example code, specificall lines 13-17. You are defining an array or function pointers, not a function with a "switch" statement.

I gave you code that compiles. Perhaps start with that and change function and variable names as appropriate.
Adding a switch statement inside of a variable definition is nonsense.
If you feel a post has answered your question, please click "Accept as Solution".

He uses a "coma", after functions inside, not dot-coma...

Oh, ok ok, I'm going to think how to do it better, =)