Skip to main content
angeliran
Associate III
March 4, 2022
Solved

How to made a function to call other functions?

  • March 4, 2022
  • 17 replies
  • 5305 views

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, =)

    This topic has been closed for replies.
    Best answer by angeliran

    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++;}

    17 replies

    TDK
    March 4, 2022

    > 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""."
    angeliran
    angeliranAuthor
    Associate III
    March 4, 2022

    Thx for answer! I will try to do it!

    S.Ma
    Principal
    March 4, 2022

    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.

    angeliran
    angeliranAuthor
    Associate III
    March 4, 2022

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

    angeliran
    angeliranAuthor
    Associate III
    March 4, 2022

    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

    TDK
    March 4, 2022
    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""."
    angeliran
    angeliranAuthor
    Associate III
    March 4, 2022

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

    Bob S
    Super User
    March 4, 2022

    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.

    angeliran
    angeliranAuthor
    Associate III
    March 4, 2022

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

    hwa
    Associate
    March 4, 2022

    It seems that all you're trying to do is copy a few literal values to one column of the 2-d array fb[], where the particular literal values you want copied depend on the value of an int (secundo or secondo_d). So my suggestion doesn't answer your question literally, since you don't need the switch statements or all the almost identical functions to do this.

    Instead, just define a table of your literal values, and one function to copy the selected values to the selected column of your array fb[].

    #define NUM_VALUES 5
     
    uint8_t value_table[][NUM_VALUES] {
     { 0x3E, 0x22, 0x22, 0x22, 0x3E }, // your values from function_0
     { 0x08, 0x18, 0x08, 0x08, 0x3E }, // your values from function_1
     ... etc.
    }
     
    void numero_x(int select) 
    {
     l = m;
     
     for (int i = 0; i < NUM_VALUES; i++) {
     fb[l+i][n] = values_table[select][i];
     }
     n++;
    }
     

    So now you just call numero_x() as often as you like, with the value you would have used in the switch statements.

    Also, I'd think about your use of globals (l, m, n), and whether incrementing n is really best done inside your function. Hope this helps.

    Cheers,

    angeliran
    angeliranAuthor
    Associate III
    March 5, 2022

    Thx for the answer! fb it´s a matriz: fb[ ][ ], and i put variables (letters and numbers), to generate a vga signal, by the spi[ ], and the corresponding syncs.

    So, i use switch function, to chose, if i need to show, 0 or 1 or 2... or 9, write to fb[ ][ ], and, when it´s time to draw, read from fb[ ] [ ], to spi tx.

    hwa
    Associate
    March 5, 2022

    Ok, but that's exactly what my numero_x() function does for you. Instead of

    switch (secundero) ...

    just call

    numero_x(secundero);

    All you need to do is fill in the rest of value_table[][]. You don't need any switch statements or a bunch of almost identical functions, and your code would not only be more compact, but more efficient also.

    hwa
    Associate
    March 6, 2022

    Look carefully at your line where you define NUM_VALUES, and compare it to the line in my example code. See the difference?

    I'm guessing you're a beginner, and learning to program by yourself (not taking any course). That's fine, everybody starts out a beginner. I'd suggest if you want to make faster progress that you get a book on introduction to programming in C, and study it very carefully, and do lots of the included exercises. It's really hard to write working programs before you have a solid understanding of the basics.

    Good luck!

    angeliran
    angeliranAuthor
    Associate III
    March 6, 2022

    Thx!!!!

    I´ve copy&paste:

    1. #define NUM_VALUES 5
    2.  
    3. uint8_t value_table[][NUM_VALUES] {
    4.  { 0x3E, 0x22, 0x22, 0x22, 0x3E }, // your values from function_0
    5.  { 0x08, 0x18, 0x08, 0x08, 0x3E }, // your values from function_1
    6.  { 0x3E, 0x22, 0x22, 0x22, 0x3E }, // your values from function_2
    7.  { 0x08, 0x18, 0x08, 0x08, 0x3E }, // your values from function_3
    8.  { 0x3E, 0x22, 0x22, 0x22, 0x3E }, // your values from function_4
    9.  
    10. }

    And the compiler trow me this error:

    1. compiling main.c...
    2. ../Src/main.c(742): error: #65: expected a ";"
    3.  uint8_t value_table[][NUM_VALUES] {
    4. ../Src/main.c(969): warning: #12-D: parsing restarts here after previous syntax error
    5.             (quita_pon=0);
    6. ../Src/main.c(970): error: #169: expected a declaration
    7.             }

    angeliran
    angeliranAuthorBest answer
    Associate III
    March 8, 2022

    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++;}